-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
306 lines (265 loc) · 9.19 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const suffix = [
{ name: '*rup', suffix: 'rup', color: 'red' },
{ name: '*by', suffix: 'by', color: 'green' },
{ name: '*huse', suffix: 'huse', color: 'rgb(139, 0, 0)' },
{ name: '*sted', suffix: 'sted', color: 'orange' },
{ name: '*lev', suffix: 'lev', color: 'yellow' },
{ name: '*skov', suffix: 'skov', color: 'greenyellow' },
{ name: '*høj', suffix: 'høj', color: 'tomato' },
{ name: '*rød', suffix: 'rød', color: 'aqua' },
{ name: '*borg', suffix: 'borg', color: 'palegoldenrod' },
{ name: '*holm', suffix: 'holm', color: 'sienna' },
{ name: '*bæk', suffix: 'bæk', color: 'peachpuff' },
{ name: '*bro', suffix: 'bro', color: 'darkturquoise' },
{ name: '*lund', suffix: 'lund', color: 'darkkhaki' },
{ name: '*løse', suffix: 'løse', color: 'mistyrose' },
{ name: '*sø', suffix: 'sø', color: 'darksalmon' },
{ name: '*havn', suffix: 'havn', color: 'rgb(0, 250, 154)' },
{ name: '*dal', suffix: 'dal', color: 'rgb(220, 220, 220)' },
];
let searchMode = false;
let input = '';
let selectedSuffixes = suffix.map((i) => i.suffix);
// detect if it is mobile
const mobile = isMobile.any;
styles = {
opacity: ['case', ['==', null, ['get', 'suffix']], 0, 0.7],
radius: ['interpolate', ['linear'], ['zoom'], 7, 2, 11, 15],
};
mapboxgl.accessToken =
'pk.eyJ1IjoiYmFmZmlvc28iLCJhIjoiY2s5cmN6ejBvMHN5OTNnbWVxcDVobmZ2bCJ9.hEIms6er4x8Jwg6LfM4tSg';
let map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/baffioso/ck9wal8yy09p11in1tuc5t814', // stylesheet location
center: [11, 55.5], // starting position [lng, lat]
zoom: 5, // starting zoom
hash: true,
});
map.on('load', () => {
map.addControl(new mapboxgl.NavigationControl(), 'bottom-left');
// create layercontrol
for (const i of suffix) {
createCheckbox(i.name, i.suffix, i.color);
}
map.addSource('byer', {
type: 'geojson',
data: 'data/bynavneendelser.geojson',
});
map.addLayer({
id: 'byer',
type: 'circle',
source: 'byer',
layout: {},
paint: {
'circle-radius': styles.radius,
'circle-color': circleColor(suffix),
'circle-opacity': styles.opacity,
},
});
map.addLayer({
id: 'byer-highlight',
type: 'circle',
source: 'byer',
layout: {},
paint: {
'circle-radius': ['interpolate', ['linear'], ['zoom'], 7, 4, 11, 15],
'circle-color': circleColor(suffix),
'circle-opacity': 1,
},
filter: ['in', 'suffix', ''],
});
map.addLayer({
id: 'byer-label',
type: 'symbol',
source: 'byer',
layout: {
'text-field': ['get', 'navn'],
'text-font': ['Open Sans Bold', 'Arial Unicode MS Bold'],
'text-size': 11,
'text-letter-spacing': 0.05,
'text-offset': [0, -1.5],
},
paint: {
'text-color': 'white',
},
filter: ['in', 'suffix', ''],
});
createPopup();
});
const createPopup = () => {
// Create a popup, but don't add it to the map yet.
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
});
map.on('mouseenter', 'byer', function (e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
var coordinates = e.features[0].geometry.coordinates.slice();
var description = e.features[0].properties.navn;
// 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;
}
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates).setHTML(description).addTo(map);
});
map.on('mouseleave', 'byer', function () {
map.getCanvas().style.cursor = '';
popup.remove();
});
};
const toggelSelectAll = () => {
let el = document.getElementById('select-all');
let checkboxes = document.querySelectorAll('input[name="legend"]');
// clear search
searchMode = false;
document.getElementById('search-input').value = '';
document.getElementById('count').innerHTML = '';
document.getElementById('count').style = 'padding-right: 0px';
if (el.checked) {
for (var checkbox of checkboxes) {
checkbox.checked = true;
selectedSuffixes = suffix.map((i) => i.suffix);
map.setFilter('byer', ['in', 'suffix'].concat(selectedSuffixes));
}
} else {
map.setFilter('byer-label', ['in', 'suffix', '']);
for (var checkbox of checkboxes) {
checkbox.checked = false;
selectedSuffixes = [];
map.setFilter('byer', ['in', 'suffix'].concat(selectedSuffixes));
map.setPaintProperty('byer', 'circle-color', circleColor(suffix));
map.setPaintProperty('byer', 'circle-opacity', [
'case',
['==', null, ['get', 'suffix']],
0,
0.7,
]);
}
}
};
const createCheckbox = (name, suffix, color) => {
// create the necessary elements
var label = document.createElement('label');
var description = document.createTextNode(name);
var checkbox = document.createElement('input');
// set element properties
label.id = suffix;
label.classList.add('noselect');
checkbox.type = 'checkbox';
checkbox.name = 'legend';
checkbox.value = suffix;
checkbox.checked = true;
// Add eventlisteners
checkbox.addEventListener('change', function (e) {
const suf = e.target.value;
if (this.checked) {
selectedSuffixes.push(suf);
map.setFilter('byer', ['in', 'suffix'].concat(selectedSuffixes));
} else {
selectedSuffixes.splice(selectedSuffixes.indexOf(suf), 1);
map.setFilter('byer', ['in', 'suffix'].concat(selectedSuffixes));
}
});
// Add hover effect depending on device
if (mobile) {
label.addEventListener('touchstart', function (e) {
const suffix = e.target.id;
highlightFeatures(suffix);
});
label.addEventListener('touchend', function () {
console.log('object');
map.setFilter('byer-highlight', ['in', 'suffix', '']);
// map.setPaintProperty('byer', 'circle-radius', styles.radius);
map.setPaintProperty('byer', 'circle-opacity', styles.opacity);
});
} else {
label.addEventListener('mouseenter', function (e) {
const suffix = e.target.id;
highlightFeatures(suffix);
});
label.addEventListener('mouseleave', function () {
map.setFilter('byer-highlight', ['in', 'suffix', '']);
// map.setPaintProperty('byer', 'circle-radius', styles.radius);
map.setPaintProperty('byer', 'circle-opacity', styles.opacity);
});
}
label.appendChild(checkbox); // add the box to the element
label.appendChild(description); // add the description to the element
// add the label element to the div
document.getElementById('suffix').appendChild(label).style.color = color;
};
const highlightFeatures = (suffix) => {
map.setFilter('byer-highlight', ['==', suffix, ['get', 'suffix']]);
map.setPaintProperty('byer', 'circle-opacity', 0.1);
};
const circleColor = (data) => {
// create mapbox style expression for markers
let style = data.reduce(
(prev, curr) => {
prev.push(curr.suffix, curr.color);
return prev;
},
['match', ['get', 'suffix']]
);
// default color
style.push('lightgray');
return style;
};
const searchFilter = () => {
searchMode = true;
input = document.getElementById('search-input').value;
let counterElem = document.getElementById('count');
if (input.length > 0) {
// Filter features and apply single style
const filter = ['in', input.toUpperCase(), ['upcase', ['get', 'navn']]];
map.setFilter('byer', filter);
map.setPaintProperty('byer', 'circle-opacity', 0.8);
map.setPaintProperty('byer', 'circle-color', 'rgb(53, 175, 109)');
// Filter labels
map.setFilter('byer-label', filter);
// Feature count
map.on('render', () => {
if (input.length > 0 && searchMode) {
const feat = map.queryRenderedFeatures({
layers: ['byer'],
filter: filter,
});
const unique = getUniqueFeatures(feat, 'id');
document.getElementById('count').innerHTML = '(' + unique.length + ')';
document.getElementById('count').style = 'padding-right: 8px';
}
});
} else {
map.setFilter('byer-label', ['in', 'suffix', '']);
map.setFilter('byer', ['in', 'suffix'].concat(selectedSuffixes));
map.setPaintProperty('byer', 'circle-color', circleColor(suffix));
map.setPaintProperty('byer', 'circle-opacity', [
'case',
['==', null, ['get', 'suffix']],
0,
0.7,
]);
document.getElementById('count').innerHTML = '';
document.getElementById('count').style = 'padding-right: 0px';
}
};
const getUniqueFeatures = (array, comparatorProperty) => {
var existingFeatureKeys = {};
// Because features come from tiled vector data, feature geometries may be split
// or duplicated across tile boundaries and, as a result, features may appear
// multiple times in query results.
var uniqueFeatures = array.filter(function (el) {
if (existingFeatureKeys[el.properties[comparatorProperty]]) {
return false;
} else {
existingFeatureKeys[el.properties[comparatorProperty]] = true;
return true;
}
});
return uniqueFeatures;
};