43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
var map = L.map('map'),
|
|
realtime = L.realtime({
|
|
url: 'locations',
|
|
crossOrigin: false,
|
|
type: 'json'
|
|
}, {
|
|
interval: 1 * 1000
|
|
}).addTo(map);
|
|
map.setView([47, 3], 5);
|
|
|
|
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 popupContent = function(fId) {
|
|
var feature = e.features[fId];
|
|
return "<h3>" + feature.properties['name'] + "</h3>\n<p>" + feature.properties['description'] + "</p>";
|
|
},
|
|
bindFeaturePopup = function(fId) {
|
|
realtime.getLayer(fId).bindPopup(popupContent(fId));
|
|
},
|
|
updateFeaturePopup = function(fId) {
|
|
realtime.getLayer(fId).getPopup().setContent(popupContent(fId));
|
|
};
|
|
|
|
// Refresh only once at the beginning
|
|
if (doFitBounds) {
|
|
doFitBounds = false;
|
|
var bounds = realtime.getBounds();
|
|
// only set if features are present
|
|
if (Object.keys(bounds).length > 0) {
|
|
map.fitBounds(realtime.getBounds(), {maxZoom: 15});
|
|
}
|
|
}
|
|
Object.keys(e.enter).forEach(bindFeaturePopup);
|
|
Object.keys(e.update).forEach(updateFeaturePopup);
|
|
}
|
|
|
|
realtime.on('update', refresh);
|