diff options
Diffstat (limited to 'web/public/javascripts/map.js')
| -rw-r--r-- | web/public/javascripts/map.js | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 190307d..5739dec 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -11,7 +11,6 @@ L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r} maxZoom: 18, ext: 'png' }).addTo(map); - L.geoJson(points, { pointToLayer: function (feature, latlng) { //return L.circleMarker(latlng); @@ -21,3 +20,34 @@ L.geoJson(points, { // layer.feature.geometry gives you access to all the fields return "<p>" + JSON.stringify(layer.feature.geometry) + "</p>"; }).addTo(map) +if (!!window.EventSource) { + var source = new EventSource('/stream'); + + source.addEventListener('message', function(e) { + var data = JSON.parse(e.data); + if (data.coordinates) { + points.push(data); + L.geoJson(points, { + pointToLayer: function (feature, latlng) { + //return L.circleMarker(latlng); + return L.marker(latlng); + } + }).bindPopup(function (layer) { + // layer.feature.geometry gives you access to all the fields + return "<p>" + JSON.stringify(layer.feature.geometry) + "</p>"; + }).addTo(map) + } + }, false) + + source.addEventListener('open', function(e) { + console.log("Connection was opened") + }, false) + + source.addEventListener('error', function(e) { + if (e.readyState == EventSource.CLOSED) { + console.log("Connection was closed") + } + }, false) +} else { + console.log("sse not supported."); +}
\ No newline at end of file |
