-
Notifications
You must be signed in to change notification settings - Fork 2
/
map-helper-functions.js
88 lines (72 loc) · 2.35 KB
/
map-helper-functions.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* This file contains functions and logic to interact with the map
* provided by the "Leaflet" library, like instantiating the map itself,
* adding and removing markers, etc.
*/
const initialLatLng = [52.5, 13.4]; // initial map center coordinate, in this case it's Berlin
const map = new L.Map("map", {
center: L.latLng(...initialLatLng),
zoom: 13,
zoomControl: false
});
const tentativeMarkers = {
pickup: L.featureGroup(),
delivery: L.featureGroup()
};
const markers = L.featureGroup();
const lines = L.featureGroup();
const initialzeMap = () => {
L.control.zoom({ position: "bottomright" }).addTo(map);
L.tileLayer(APIEndpoints.GetImageTiles, {
attribution: "© " + new Date().getFullYear() + ", PTV Logistics, HERE",
minZoom: 5,
maxZoom: 23
}, [
{header: 'apiKey', value: api_key}
]).addTo(map);
tentativeMarkers.pickup.addTo(map);
tentativeMarkers.delivery.addTo(map);
markers.addTo(map);
lines.addTo(map);
};
const addTentativeMarkerToMap = (latitude, longitude, serviceType) => {
tentativeMarkers[serviceType].clearLayers();
const marker = L.marker([latitude, longitude])
marker.addTo(tentativeMarkers[serviceType]);
map.panTo([latitude, longitude]);
};
const addMarkerToMap = (latitude, longitude) => {
const marker = L.marker([latitude, longitude])
marker.addTo(markers);
map.panTo([latitude, longitude]);
};
const clearTentativeMarkers = () => {
tentativeMarkers.pickup.clearLayers();
tentativeMarkers.delivery.clearLayers();
};
const clearAllLines = () => lines.clearLayers();
const clearAllMarkers = () => {
clearAllLines();
markers.clearLayers();
};
const addPolylineToMap = (coordinates) => {
const randomColor = "#" + (Math.random() * 0xFFFFFF << 0).toString(16).padStart(6, "0");
const polyline = L.polyline(coordinates, {color: randomColor});
const decorator = L.polylineDecorator(polyline, {
patterns: [{
offset: '100%',
repeat: 0,
symbol: L.Symbol.arrowHead({
pixelSize: 20,
polygon: false,
pathOptions: {
stroke: true,
color: randomColor
}
})
}]
})
polyline.addTo(lines);
decorator.addTo(lines);
map.fitBounds(markers.getBounds());
}