This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
55 lines (50 loc) · 1.44 KB
/
map.js
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
54
55
const qs = require('querystring');
const markers = [];
function removeMarkers(map) {
while (markers.length) {
map.removeLayer(markers.pop());
}
}
function fetchData(map) {
const { lng, lat } = map.getCenter();
const params = {
longitude: lng,
latitude: lat,
radius: 1000000,
};
fetch(`/bahnparking?${qs.stringify(params)}`, {
})
.then((response) => {
if (response.status !== 200) {
throw new Error(`Provider failed with ${response.status}: ${response.statusText}`);
}
return response.json();
})
.then((result) => {
removeMarkers(map);
result.forEach((parking) => {
const marker = new L.Marker(new L.LatLng(parking.latitude, parking.longitude), {
icon: L.BeautifyIcon.icon({
isAlphaNumericIcon: true,
text: parking.vacant,
}),
});
map.addLayer(marker);
markers.push(marker);
});
})
.catch((e) => {
console.log(e.message);
});
}
function prepareMap() {
const map = L.map('map');
const osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const osmAttrib = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
const osm = new L.TileLayer(osmUrl, {minZoom: 6, maxZoom: 24, attribution: osmAttrib});
map.setView(new L.LatLng(50.9375, 6.9603), 9);
map.addLayer(osm);
map.on('moveend', _ => fetchData(map));
fetchData(map);
}
document.addEventListener('DOMContentLoaded', prepareMap, false);