-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
100 lines (77 loc) · 2.43 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
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
89
90
91
92
93
94
95
96
97
98
99
100
var MapHelper = (function () {
const leafletReference = L;
let _divMap;
let _myMap;
let _markersCurrentlyOnMap = [];
// init values
const INIT_LATITUDE = 46.2276;
const INIT_LONGITUDE = 2.2137;
const INIT_ZOOM = 4;
// layer options
const minZoom = 1;
const maxZoom = 18;
const tileLayer = 'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}{r}.png';
const attribution = '<a href="https://wikimediafoundation.org/wiki/Maps_Terms_of_Use">Wikimedia</a>';
function init(divMap) {
_divMap = divMap;
_myMap = leafletReference.map(_divMap);
setup();
}
function setup() {
leafletReference.tileLayer(tileLayer, {
attribution: attribution,
minZoom: minZoom,
maxZoom: maxZoom
}).addTo(_myMap);
}
function addMarker(coordinate, isOwnPosition) {
if (coordinate === undefined)
return;
let marker;
if (isOwnPosition === true) {
marker = leafletReference.marker(coordinate.getCoordinates(), {
icon: createRedIcon()
});
} else {
marker = leafletReference.marker(coordinate.getCoordinates());
marker.bindPopup(coordinate.description);
}
_markersCurrentlyOnMap.push(marker.addTo(_myMap));
}
function addMarkers(coordinates, shouldHighlight) {
for (let coordinate of coordinates) {
addMarker(coordinate, shouldHighlight);
}
}
function createRedIcon() {
return new L.Icon({
iconUrl: 'leaflet/images/marker-icon-red.png',
shadowUrl: 'leaflet/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
}
function cleanMarkers() {
for (let marker of _markersCurrentlyOnMap) {
marker.remove();
}
_markersCurrentlyOnMap = [];
}
function setView(latitude, longitude, zoom) {
const lat = latitude || INIT_LATITUDE;
const lon = longitude || INIT_LONGITUDE;
const z = zoom || INIT_ZOOM;
_myMap.setView(leafletReference.latLng(lat, lon), z, {
animation: true
});
}
return {
init: init,
addMarker: addMarker,
addMarkers: addMarkers,
cleanMarkers: cleanMarkers,
setView: setView
}
})();