-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_bici_real_time.js
71 lines (58 loc) · 1.98 KB
/
get_bici_real_time.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
getBiciRealTime();
async function getBiciRealTime() {
const response = await fetch('/bici_real_time');
const data = await response.json();
if (!window.abortLoopBikesRealTime) {
addRealTime(data);
}
}
async function addRealTime(data) {
var jsonFeatures = [];
const responseBiciFuoriRange = await fetch('/bici_fuori_range?distanza=' + distanza);
const biciFuoriRange = await responseBiciFuoriRange.json();
data.forEach(function (point) {
var lat = point.lat;
var lon = point.long;
var id = point.id;
var feature = {
type: 'Feature',
properties: {
lat: lat,
lon: lon,
id: id
},
geometry: {
type: 'Point',
coordinates: [lon, lat]
},
};
jsonFeatures.push(feature);
});
var geoJson = {type: 'FeatureCollection', features: jsonFeatures};
window.layerBiciRealTime = L.geoJson(geoJson, {
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {
icon: getIcon(feature),
});
},
});
function getIcon(feature) {
var color = 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png';
for (var bici of biciFuoriRange) {
if (bici.id === feature.properties.id) {
color = 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png'
break;
}
}
return (new L.Icon({
iconUrl: color,
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
}));
}
mymap.removeLayer(window.layerBiciRealTime);
window.layerBiciRealTime.addTo(mymap);
}