/
worldview.js
123 lines (101 loc) · 3.37 KB
/
worldview.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
113
114
115
116
117
118
119
120
121
122
123
// Just in case I guess
$(window).load(function() {
// alextodo, markers and coordinates all need to be removed from map
// and newly assigned after every new ajax request because otherwise they will grow too large
var markers = new Array(),
coordinates,
map,
i,
callBackInterval = 60,
showInterval = 1,
numberOfCordinatesShownAtOneTime = 1,
ticks = 0,
nextShowInterval = 0,
setupMarkersTime = 1,
// test with 10 seconds, real should be 180 (3 minutes?)
clearMarkersTime = 20;
// the time markers should be shown
// alextodo clear the nextShowInterval time on call back from server
function setUpMarkers() {
if (((ticks === 0) || (ticks === nextShowInterval)) && coordinates.length > 0) {
for( i = 0; i < numberOfCordinatesShownAtOneTime; i++ ) {
var coordinate = coordinates.pop(),
image = "pin.png";
// Only calling this once?
//console.log("Setting a marker");
var marker = new google.maps.Marker({
position: coordinate,
icon: image,
optimized: false,
map: map
});
marker.setAnimation(google.maps.Animation.DROP);
marker.ticks = ticks;
marker.setVisible(true);
markers.push(marker);
};
nextShowInterval += showInterval;
}
ticks++;
};
function setUpMap() {
var centerOfTheWorld = new google.maps.LatLng(40.97989806962013, -31.640625);
var options = {
zoom: 2,
scrollwheel: false,
disableDoubleClickZoom: true,
zoomControl: false,
center: centerOfTheWorld,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
};
function getCoordinates() {
//$.getJSON('http://css-tricks.com/wufoo/dummydata/dummydata.json?callback=?', function(data) {
$.getJSON('http://pink.eye:8000?callback=?', function(data) {
coordinates = new Array();
for(var index in data.coordinates) {
var coordinate = data.coordinates[index];
coordinates.push(new google.maps.LatLng(coordinate.latitude, coordinate.longitude));
}
calculateIntervals();
setUpMarkers();
});
};
function calculateIntervals() {
if(coordinates.length > callBackInterval) {
showInterval = 1;
numberOfCordinatesShownAtOneTime = Math.ceil(coordinates.length / callBackInterval);
}
else {
numberOfCordinatesShownAtOneTime = 1;
coordinatesLength = (coordinates.length > 0) ? coordinates.length : 1;
showInterval = Math.floor(callBackInterval / coordinatesLength);
}
// After the initial data request, jump start the nextShowInterval, skip 0
// as a show interval because of time lapse
if(nextShowInterval == 0) {
nextShowInterval += showInterval;
}
}
function clearMarkers() {
for ( i=0; i < markers.length; i++) {
if((markers[i].ticks + clearMarkersTime) < ticks || markers[i].ticks == callBackInterval) {
markers[i].setVisible(false);
}
}
}
function pollServer() {
if(ticks >= callBackInterval) {
markers = new Array();
ticks = 0;
nextShowInterval = 0;
getCoordinates();
}
}
setUpMap();
getCoordinates();
setInterval(pollServer, 1000);
setInterval(setUpMarkers, setupMarkersTime * 1000);
setInterval(clearMarkers, clearMarkersTime * 1000);
});