-
Notifications
You must be signed in to change notification settings - Fork 0
/
erasmus.js
112 lines (94 loc) · 4.04 KB
/
erasmus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var jsonPath = "students.php"
var map;
var geocoder;
var queryTries = 50;
var queryTimeout = 150;
var totalMarkers = 0;
var numberStudents = 0;
var univNames = new Object();
var univArray = new Object();
var markersContent = new Object();
function initialize() {
var myOptions = {
center: new google.maps.LatLng(41.1782, -8.5956), // FEUP
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder = new google.maps.Geocoder();
}
function loadPoints() {
$.ajax({
url: jsonPath,
dataType: 'json',
data: '',
success: function (data) {
console.log('Data extracted: \n' + data);
console.log('\n ' + data.length + ' students ');
numberStudents = data.length - 1;
$.each(data, function (index, student) {
var university = univNames[student[3]];
if (university == null) {
console.log('null univ\n');
var timeout = queryTimeout;
setTimeout( function() { addMarker(student, timeout); }, timeout);
}
else // Name exists, marker exists
addStudent(university, student); // Almost always this is skipped
});
},
error: function (data) {
console.log('Error loading markers' + data);
}
});
}
function addStudent(universityName, student) {
markersContent[universityName] += '<p> ' + student[0] + ' - ' + student[1] + '</p>';
console.log('(' + totalMarkers + '/' + numberStudents + ')' + ' Adding ' + student[0] + ' to ' + student[3] + ', ' + student[2] + ' ...');
totalMarkers++;
}
function addMarker( student , timeout) {
var university = univNames[student[3]]; // In the meantime, maybe we have the univ information?
if (university != null) {
addStudent(university, student);
return;
}
geocoder.geocode({ 'address': student[3] }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var universityName = results[0].address_components[0].long_name;
univNames[student[3]] = universityName;
if (!univArray.hasOwnProperty(universityName)) { // Do we have a marker already?
univArray[universityName] = '';
console.log('(--/--) Adding University ' + universityName + ' to ' + student[2] + ' ...');
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: universityName + ', ' + student[2],
animation: google.maps.DROP
});
var infowindow = new google.maps.InfoWindow({ content: '' });
markersContent[universityName] = '';
google.maps.event.addListener(marker, "click", function () { // Click event, dynamically create content, open window
var contentString0 = '<div class="university" id="' + universityName + '">' +
'<div id="siteNotice">' +
'</div>' +
'<h2 id="firstHeading" class="firstHeading">' + universityName + ', ' + student[2] + '</h2>' +
'<div id="bodyContent">'
var contentString1 = '</div>' +
'</div>';
infowindow.setContent(contentString0 + markersContent[universityName] + contentString1);
map.panTo(results[0].geometry.location);
infowindow.open(map, marker);
});
}
addStudent(univNames[student[3]], student);
}
else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
console.log('(--/--) Error adding ' + student[0]);
totalMarkers++;
}
else {
setTimeout(function () { addMarker(student, queryTimeout); }, queryTimeout);
}
});
}