-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-map.js
115 lines (88 loc) · 2.92 KB
/
leaflet-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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Convert POIs (Points of Interest) from plain JSON to GeoJSON
*/
function toGeoJson(pois) {
var geoJson = {
"type": "FeatureCollection",
"features": []
};
pois.forEach(element => {
geoJson.features.push({
"type": "Feature",
"properties": element,
"geometry": {
"type": "Point",
"coordinates": [
element.lon,
element.lat
]
}
});
});
return geoJson;
}
/**
* Overlay (vector) Layers
*/
// Display more details of the selected feature on a side panel
function displayMoreInfo(e) {
featureProps = e.target.feature.properties;
var html = "";
html = '<strong>ID : </strong>' + featureProps.id + '<br/>';
html += '<strong>City : </strong>' + featureProps.city + '<br/>';
document.getElementById('more').innerHTML = html;
console.log(e.target.feature.properties);
}
// specify popup options | popupCustom is defined in the HTML style section
var customPopupOptions =
{
'maxWidth': '400',
'width': '200',
'className': 'popupCustom'
}
// Config event handlers for the Overlay Layer features
function onEachFeature(feature, layer) {
//Replace POIs property with yours
if (feature.properties && feature.properties.id) {
let popupContent = 'ID : ' + feature.properties.id + "<br/>City: " + feature.properties.city;
layer.bindPopup(popupContent, customPopupOptions);
}
// Add listner to diplay more informations in an external pane
layer.on({
click: displayMoreInfo
});
}
// Create a new overlay layer as a geoJSON
var layerPOIs = L.geoJSON([toGeoJson(pois)], {
onEachFeature: onEachFeature
});
/**
* Base Layers
*/
// Create Base layer from OpenStreetMap tiles
var layerOSM = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
tileSize: 512,
zoomOffset: -1
});
// Create Base layer from OpenTopoMap tiles
var layerOpenTopoMap = L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {
maxZoom: 17,
attribution: 'Map data: © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: © <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
});
/**
* Map initialisation and Add the layers to it
*/
var map = L.map('map', { layers: [layerPOIs] }).setView([28.0, 3.0], 5);
map.addLayer(layerOSM);
/**
* Add Base and overlay layers to layers control panel (top-right)
*/
var baseLayers = {
"OpenStreetMap": layerOSM,
"OpenTopoMap": layerOpenTopoMap
};
var overlays = {
"Points of Interest": layerPOIs
};
L.control.layers(baseLayers, overlays).addTo(map);