-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
345 lines (310 loc) · 9.56 KB
/
index.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Expand cluster leaves on click (spiderify) using mapbox marker or a circle layer
// Expanded cluster reset on click and on zoom
// See options below to customize behaviour
// Known bugs
// Double clicking on cluster zoom and spiderify zoom - 1
// Based on Mapbox cluster example https://docs.mapbox.com/mapbox-gl-js/example/cluster/1
// Spiral credits
// http://jsfiddle.net/gjowrfcd/1/
// http://jsfiddle.net/uh1rLvj2/
// Circle credits
// https://stackoverflow.com/questions/24273990/calculating-evenly-spaced-points-on-the-perimeter-of-a-circle
mapboxgl.accessToken = ""; // Your Mapbox token here
const SOURCE_EARTHQUAKE = "earthquakes";
const SPIDERIFY_AFTER_ZOOM = 4; // Spiderify after zoom N, zoom otherwise
const SPIDER_TYPE = "layer"; // marker: use Mapbox's Marker. layer: Use a Mabpbox point layer
const MAX_LEAVES_TO_SPIDERIFY = 255; // Max leave to display when spiderify to prevent filling the map with leaves
const CIRCLE_TO_SPIRAL_SWITCHOVER =
SPIDER_TYPE.toLowerCase() === "marker" ? 10 : 15; // When below number, will display leave as a circle. Over, as a spiral
const CIRCLE_OPTIONS = {
distanceBetweenPoints: 50
};
const SPIRAL_OPTIONS = {
rotationsModifier: 1250, // Higher modifier = closer spiral lines
distanceBetweenPoints: SPIDER_TYPE.toLowerCase() === "marker" ? 42 : 32, // Distance between points in spiral
radiusModifier: 50000, // Spiral radius
lengthModifier: 1000 // Spiral length modifier
};
const SPIDER_LEGS = true;
const SPIDER_LEGS_LAYER_NAME = `spider-legs-${Math.random()
.toString(36)
.substr(2, 9)}`;
const SPIDER_LEGS_PAINT_OPTION = {
"line-width": 3,
"line-color": "rgba(128, 128, 128, 0.5)"
};
const SPIDER_LEAVES_LAYER_NAME = `spider-leaves-${Math.random()
.toString(36)
.substr(2, 9)}`;
const SPIDER_LEAVES_PAINT_OPTION = {
"circle-color": "orange",
"circle-radius": 6,
"circle-stroke-width": 1,
"circle-stroke-color": "#fff"
};
let clusterMarkers = [];
let spiderifiedCluster = {};
let map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/dark-v10",
center: [-103.59179687498357, 40.66995747013945],
zoom: 3
});
function clearSpiderifiedMarkers() {
if (clusterMarkers.length > 0) {
for (let i = 0; i < clusterMarkers.length; i++) {
clusterMarkers[i].remove();
}
}
clusterMarkers = [];
}
function removeSourceAndLayer(map, id) {
if (map.getLayer(id) != null) map.removeLayer(id);
if (map.getSource(id) != null) map.removeSource(id);
}
function clearSpiderifiedCluster() {
spiderifiedCluster = {};
spiderLeavesCollection = [];
removeSourceAndLayer(map, SPIDER_LEGS_LAYER_NAME);
removeSourceAndLayer(map, SPIDER_LEAVES_LAYER_NAME);
clearSpiderifiedMarkers();
}
function generateEquidistantPointsInCircle({
totalPoints = 1,
options = CIRCLE_OPTIONS
}) {
let points = [];
let theta = (Math.PI * 2) / totalPoints;
let angle = theta;
for (let i = 0; i < totalPoints; i++) {
angle = theta * i;
points.push({
x: options.distanceBetweenPoints * Math.cos(angle),
y: options.distanceBetweenPoints * Math.sin(angle)
});
}
return points;
}
function generateEquidistantPointsInSpiral({
totalPoints = 10,
options = SPIRAL_OPTIONS
}) {
let points = [{ x: 0, y: 0 }];
// Higher modifier = closer spiral lines
const rotations = totalPoints * options.rotationsModifier;
const distanceBetweenPoints = options.distanceBetweenPoints;
const radius = totalPoints * options.radiusModifier;
// Value of theta corresponding to end of last coil
const thetaMax = rotations * 2 * Math.PI;
// How far to step away from center for each side.
const awayStep = radius / thetaMax;
for (
let theta = distanceBetweenPoints / awayStep;
points.length <= totalPoints + options.lengthModifier;
) {
points.push({
x: Math.cos(theta) * (awayStep * theta),
y: Math.sin(theta) * (awayStep * theta)
});
theta += distanceBetweenPoints / (awayStep * theta);
}
return points.slice(0, totalPoints);
}
function generateLeavesCoordinates({ nbOfLeaves }) {
// Position cluster's leaves in circle if below threshold, spiral otherwise
if (nbOfLeaves < CIRCLE_TO_SPIRAL_SWITCHOVER) {
points = generateEquidistantPointsInCircle({
totalPoints: nbOfLeaves
});
} else {
points = generateEquidistantPointsInSpiral({
totalPoints: nbOfLeaves
});
}
return points;
}
function spiderifyCluster({ map, source, clusterToSpiderify }) {
let spiderlegsCollection = [];
let spiderLeavesCollection = [];
map
.getSource(source)
.getClusterLeaves(
clusterToSpiderify.id,
MAX_LEAVES_TO_SPIDERIFY,
0,
(error, features) => {
if (error) {
console.warning("Cluster does not exists on this zoom");
return;
}
let leavesCoordinates = generateLeavesCoordinates({
nbOfLeaves: features.length
});
let clusterXY = map.project(clusterToSpiderify.coordinates);
// Generate spiderlegs and leaves coordinates
features.forEach((element, index) => {
let spiderLeafLatLng = map.unproject([
clusterXY.x + leavesCoordinates[index].x,
clusterXY.y + leavesCoordinates[index].y
]);
if (SPIDER_TYPE.toLowerCase() === "marker") {
clusterMarkers.push(
new mapboxgl.Marker().setLngLat(spiderLeafLatLng)
);
}
if (SPIDER_TYPE.toLowerCase() === "layer") {
spiderLeavesCollection.push({
type: "Feature",
geometry: {
type: "Point",
coordinates: [spiderLeafLatLng.lng, spiderLeafLatLng.lat]
}
});
}
if (SPIDER_LEGS) {
spiderlegsCollection.push({
type: "Feature",
geometry: {
type: "LineString",
coordinates: [
clusterToSpiderify.coordinates,
[spiderLeafLatLng.lng, spiderLeafLatLng.lat]
]
}
});
}
});
// Draw spiderlegs and leaves coordinates
if (SPIDER_LEGS) {
map.addLayer({
id: SPIDER_LEGS_LAYER_NAME,
type: "line",
source: {
type: "geojson",
data: {
type: "FeatureCollection",
features: spiderlegsCollection
}
},
paint: SPIDER_LEGS_PAINT_OPTION
});
}
if (SPIDER_TYPE.toLowerCase() === "marker") {
clusterMarkers.forEach(marker => marker.addTo(map));
}
if (SPIDER_TYPE.toLowerCase() === "layer") {
map.addLayer({
id: SPIDER_LEAVES_LAYER_NAME,
type: "circle",
source: {
type: "geojson",
data: {
type: "FeatureCollection",
features: spiderLeavesCollection
}
},
paint: SPIDER_LEAVES_PAINT_OPTION
});
}
}
);
}
map.on("load", () => {
map
.on("click", "clusters", e => {
let features = map.queryRenderedFeatures(e.point, {
layers: ["clusters"]
});
let clusterId = features[0].properties.cluster_id;
// Zoom on cluster or spiderify it
if (map.getZoom() < SPIDERIFY_AFTER_ZOOM) {
map
.getSource(SOURCE_EARTHQUAKE)
.getClusterExpansionZoom(clusterId, (err, zoom) => {
if (err) return;
map.easeTo({
center: features[0].geometry.coordinates,
zoom: zoom
});
});
} else {
spiderifiedCluster = {
id: clusterId,
coordinates: features[0].geometry.coordinates
};
spiderifyCluster({
map: map,
source: SOURCE_EARTHQUAKE,
clusterToSpiderify: spiderifiedCluster
});
}
})
.on("click", e => {
clearSpiderifiedCluster();
})
.on("zoomstart", () => {
clearSpiderifiedCluster();
});
// From https://docs.mapbox.com/mapbox-gl-js/example/cluster/
// Start of Mapbox cluster example
map.addSource(SOURCE_EARTHQUAKE, {
type: "geojson",
data: "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson",
cluster: true,
clusterMaxZoom: 14, // Max zoom to cluster points on
clusterRadius: 50 // Radius of each cluster when clustering points (defaults to 50)
});
map.addLayer({
id: "clusters",
type: "circle",
source: SOURCE_EARTHQUAKE,
filter: ["has", "point_count"],
paint: {
"circle-color": [
"step",
["get", "point_count"],
"#51bbd6",
100,
"#f1f075",
750,
"#f28cb1"
],
"circle-radius": ["step", ["get", "point_count"], 20, 100, 30, 750, 40]
}
});
map.addLayer({
id: "cluster-count",
type: "symbol",
source: SOURCE_EARTHQUAKE,
filter: ["has", "point_count"],
layout: {
"text-field": "{point_count_abbreviated}",
"text-size": 12
}
});
map.addLayer({
id: "unclustered-point",
type: "circle",
source: SOURCE_EARTHQUAKE,
filter: ["!", ["has", "point_count"]],
paint: {
"circle-color": "#11b4da",
"circle-radius": 4,
"circle-stroke-width": 1,
"circle-stroke-color": "#fff"
}
});
map.addControl(
new mapboxgl.ScaleControl({
maxWidth: 80,
unit: "metric"
})
);
map.on(
"mouseenter",
"clusters",
() => (map.getCanvas().style.cursor = "pointer")
);
map.on("mouseleave", "clusters", () => (map.getCanvas().style.cursor = ""));
// End of Mapbox cluster example
});