aboutsummaryrefslogtreecommitdiff
path: root/web/public/javascripts/map.js
blob: 5739dec3e3c6718af7e618f61bb348be75a67083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// different basemap
// L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
//     attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
// }).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> &mdash; Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
	subdomains: 'abcd',
	minZoom: 0,
	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)
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.");
}