-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (86 loc) · 3.14 KB
/
app.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
//SRC: https://stackoverflow.com/questions/51033188/how-to-use-supercluster
var map = L.map('map').setView([0, 0], 0);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Empty Layer Group that will receive the clusters data on the fly.
var markers = L.geoJSON(null, {
pointToLayer: createClusterIcon,
onEachFeature: function onEachFeature(feature, layer) {
// does this feature have a property named popupContent?
if (feature.properties && feature.properties.id) {
layer.bindPopup("<b>"+feature.properties.id+"</b>");
}
}
}).addTo(map);
// Update the displayed clusters after user pan / zoom.
map.on('moveend', update);
function update() {
if (!ready) return;
var bounds = map.getBounds();
var bbox = [bounds.getWest(), bounds.getSouth(), bounds.getEast(), bounds.getNorth()];
var zoom = map.getZoom();
var clusters = index.getClusters(bbox, zoom);
markers.clearLayers();
markers.addData(clusters);
}
// Zoom to expand the cluster clicked by user.
markers.on('click', function(e) {
var clusterId = e.layer.feature.properties.cluster_id;
var center = e.latlng;
var expansionZoom;
if (clusterId) {
expansionZoom = index.getClusterExpansionZoom(clusterId);
map.flyTo(center, expansionZoom);
}
});
// Retrieve Points data.
const generateMarkers = (count) => {
const southWest = new L.latLng(39.60463011823322, -105.0667190551758);
const northEast = new L.latLng(39.68393975392733, -104.90947723388673);
const bounds = new L.latLngBounds(southWest, northEast);
const minLat = bounds.getSouthWest().lat,
rangeLng = bounds.getNorthEast().lat - minLat,
minLng = bounds.getSouthWest().lng,
rangeLat = bounds.getNorthEast().lng - minLng;
const result = {};
result.features = Array.from({ length: count }, (v, k) => {
return {
type: "Feature",
properties:{
id: k,
},
geometry:{
type:"Point",
coordinates: [minLng + Math.random() * rangeLat, minLat + Math.random() * rangeLng]
},
};
});
return result;
};
const geojson = generateMarkers(500000);
//var placesUrl = 'https://cdn.rawgit.com/mapbox/supercluster/v4.0.1/test/fixtures/places.json';
//var index;
//var ready = false;
index = supercluster({
radius: 60,
extent: 256,
maxZoom: 18
}).load(geojson.features); // Expects an array of Features.
ready = true;
update();
function createClusterIcon(feature, latlng) {
if (!feature.properties.cluster) {return L.marker(latlng);console.log(latlng)}
var count = feature.properties.point_count;
var size =
count < 100 ? 'small' :
count < 1000 ? 'medium' : 'large';
var icon = L.divIcon({
html: '<div><span>' + feature.properties.point_count_abbreviated + '</span></div>',
className: 'marker-cluster marker-cluster-' + size,
iconSize: L.point(40, 40)
});
return L.marker(latlng, {
icon: icon
});
}