-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbusses.js
More file actions
42 lines (36 loc) · 1.27 KB
/
busses.js
File metadata and controls
42 lines (36 loc) · 1.27 KB
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
var map = L.map('map', {
center: [-0.427781, 36.943359], zoom: 20
}
),
realtime = L.realtime(undefined, {
getFeatureId: function(f) { return f.id; },
start: false
}).addTo(map);
function update(e) {
realtime.update(JSON.parse(e.data));
}
function remove(e) {
realtime.remove(JSON.parse(e.data));
}
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var source = new EventSource('http://api.chab.us/buses/tail');
source.addEventListener('add', update);
source.addEventListener('change', update);
source.addEventListener('remove', remove);
realtime.on('update', function(e) {
var popupContent = function(fId) {
var feature = e.features[fId];
return '<h3>' + feature.properties.route + '</h3>' +
feature.properties.routeDirection;
},
bindFeaturePopup = function(fId) {
realtime.getLayer(fId).bindPopup(popupContent(fId));
},
updateFeaturePopup = function(fId) {
realtime.getLayer(fId).getPopup().setContent(popupContent(fId));
};
Object.keys(e.enter).forEach(bindFeaturePopup);
Object.keys(e.update).forEach(updateFeaturePopup);
});