diff options
| author | Ivan Shen <iwshen11@gmail.com> | 2019-03-05 21:48:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-05 21:48:34 +0000 |
| commit | 7ea91f912c1511f60466c3a47ad6ee075cecc0f2 (patch) | |
| tree | 03121db88872a58363eed00703ad57f2151918fd /web/public | |
| parent | b6e80aee2f946949165ef6fcc6f0c433c60123b9 (diff) | |
| parent | a5ff29d43a952ebe2fcc18181a01beda0e5d62d2 (diff) | |
Merge pull request #24 from csc301-winter-2019/feat/8
Add live updates functionality
Diffstat (limited to 'web/public')
| -rw-r--r-- | web/public/javascripts/map.js | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 190307d..4b8b8f3 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -3,6 +3,18 @@ // attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> 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 "<p>" + JSON.stringify(layer.feature.geometry) + "</p>"; + }).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 <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', @@ -11,13 +23,27 @@ 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 "<p>" + JSON.stringify(layer.feature.geometry) + "</p>"; -}).addTo(map) +plotPointsOnMap(points); +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); + plotPointsOnMap(points); + } + }, 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."); +} |
