41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
var map = L.map('map'),
|
|
realtime = L.realtime({
|
|
url: 'locations',
|
|
crossOrigin: false,
|
|
type: 'json'
|
|
}, {
|
|
interval: 1 * 1000
|
|
}).addTo(map);
|
|
|
|
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
|
|
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
|
|
}).addTo(map);
|
|
|
|
var doFitBounds = true;
|
|
function refresh(e) {
|
|
var coordPart = function(v, dirs) {
|
|
return dirs.charAt(v >= 0 ? 0 : 1) +
|
|
(Math.round(Math.abs(v) * 100) / 100).toString();
|
|
},
|
|
popupContent = function(fId) {
|
|
var feature = e.features[fId],
|
|
c = feature.geometry.coordinates;
|
|
return feature.properties['name' ] + ' at ' + coordPart(c[1], 'NS') + ', ' + coordPart(c[0], 'EW');
|
|
},
|
|
bindFeaturePopup = function(fId) {
|
|
realtime.getLayer(fId).bindPopup(popupContent(fId));
|
|
},
|
|
updateFeaturePopup = function(fId) {
|
|
realtime.getLayer(fId).getPopup().setContent(popupContent(fId));
|
|
};
|
|
|
|
if (doFitBounds) {
|
|
map.fitBounds(realtime.getBounds(), {maxZoom: 15});
|
|
doFitBounds = false;
|
|
}
|
|
Object.keys(e.enter).forEach(bindFeaturePopup);
|
|
Object.keys(e.update).forEach(updateFeaturePopup);
|
|
}
|
|
|
|
realtime.on('update', refresh);
|