From 0a573cc3e918e751f5a9e02f5877f7786636539d Mon Sep 17 00:00:00 2001 From: ivanshen Date: Tue, 5 Mar 2019 15:58:03 -0500 Subject: Added live update functionality (feat 8) --- web/public/javascripts/map.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'web/public/javascripts/map.js') 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 "

" + JSON.stringify(layer.feature.geometry) + "

"; }).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 "

" + JSON.stringify(layer.feature.geometry) + "

"; + }).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 -- cgit v1.2.3 From 3b36b1ad459ee739a87a32a18bb5846770125bec Mon Sep 17 00:00:00 2001 From: ivanshen Date: Tue, 5 Mar 2019 16:39:35 -0500 Subject: modularize plot points --- web/public/javascripts/map.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'web/public/javascripts/map.js') diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 5739dec..2547780 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -3,6 +3,18 @@ // attribution: '© OpenStreetMap contributors' // }).addTo(map); +function plotPointsOnMap(points) { + 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 "

" + JSON.stringify(layer.feature.geometry) + "

"; + }).addTo(map) +} + // different basemap L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r}.{ext}', { attribution: 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors', @@ -11,15 +23,7 @@ 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); - return L.marker(latlng); - } -}).bindPopup(function (layer) { - // layer.feature.geometry gives you access to all the fields - return "

" + JSON.stringify(layer.feature.geometry) + "

"; -}).addTo(map) +plotPointsOnMap(points); if (!!window.EventSource) { var source = new EventSource('/stream'); @@ -27,15 +31,7 @@ if (!!window.EventSource) { 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 "

" + JSON.stringify(layer.feature.geometry) + "

"; - }).addTo(map) + plotPointsOnMap(points); } }, false) -- cgit v1.2.3 From a5ff29d43a952ebe2fcc18181a01beda0e5d62d2 Mon Sep 17 00:00:00 2001 From: Ivan Shen Date: Tue, 5 Mar 2019 16:41:47 -0500 Subject: remove unnecessary !! --- web/public/javascripts/map.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'web/public/javascripts/map.js') diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 2547780..4b8b8f3 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -24,7 +24,7 @@ L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r} ext: 'png' }).addTo(map); plotPointsOnMap(points); -if (!!window.EventSource) { +if (window.EventSource) { var source = new EventSource('/stream'); source.addEventListener('message', function(e) { @@ -46,4 +46,4 @@ if (!!window.EventSource) { }, false) } else { console.log("sse not supported."); -} \ No newline at end of file +} -- cgit v1.2.3