-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
139 lines (128 loc) · 3.47 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
---
mapboxgl.accessToken = "{{site.mapboxtoken}}";
const filterGroup = document.getElementById('filter-group');
const map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/light-v10",
zoom: 7,
center: [77.61588870000003, 12.9346911],
});
map.on("idle", function () {
map.resize();
});
map.on("load", (e) => {
map.resize();
map.addSource("plugo", {
type: "geojson",
data: "map.geojson",
data: "/plugo/map.geojson",
});
map.addLayer({
id: "markers-zero",
type: "symbol",
source: "plugo",
filter: ['==', 'count', 0],
layout: {
"icon-allow-overlap": true,
"icon-ignore-placement": true,
"text-allow-overlap": false,
"icon-size": 1.3,
"icon-image": ["get", "icon"],
"text-variable-anchor": ["bottom", "right"],
"text-radial-offset": 0.7,
"text-field": ["get", "shorttitle"],
"visibility": "none",
"text-size": {
stops: [
[0, 0],
[12, 0],
[12.99999999, 0],
[13.0000001, 10],
[15, 12],
[20, 15],
],
},
},
paint: {
"text-color": "#202",
"text-halo-color": "#ff5855",
"text-halo-blur": 1,
"text-halo-width": 2,
},
});
map.addLayer({
id: "markers",
type: "symbol",
source: "plugo",
filter: ['!=', 'count', 0],
layout: {
"icon-allow-overlap": true,
"icon-ignore-placement": true,
"text-allow-overlap": true,
"icon-size": 1.3,
"icon-image": ["get", "icon"],
"text-variable-anchor": ["bottom", "right"],
"text-radial-offset": 0.7,
"text-field": ["get", "shorttitle"],
"text-size": {
stops: [
[0, 0],
[12, 0],
[12.99999999, 0],
[13.0000001, 10],
[15, 12],
[20, 15],
],
},
},
paint: {
"icon-color": "#00ff00",
"text-color": "#202",
"text-halo-color": "#ebff32",
"text-halo-blur": 1,
"text-halo-width": 2,
},
});
// When the checkbox changes, update the visibility of the layer.
M = document.getElementById('markers-zero')
M.addEventListener('change', (e) => {
map.setLayoutProperty(
'markers-zero',
'visibility',
e.target.checked ? 'visible' : 'none'
);
})
function onclick(e) {
// Copy coordinates array.
console.log(e.features)
const coordinates = e.features[0].geometry.coordinates.slice();
const P = e.features[0].properties;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(
`${P.description}<br>${P.count} powerbanks available<br>Location Type: ${P.type}`
)
.addTo(map);
}
map.on("click", "markers", onclick);
map.on("click", "markers-zero", onclick);
map.addControl(new mapboxgl.FullscreenControl());
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true,
},
// When active the map will receive updates to the device's location as it changes.
trackUserLocation: true,
// Draw an arrow next to the location dot to indicate which direction the device is heading.
showUserHeading: true,
})
);
});