-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathreact-leaflet-markercluster.js
More file actions
56 lines (47 loc) · 1.42 KB
/
react-leaflet-markercluster.js
File metadata and controls
56 lines (47 loc) · 1.42 KB
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
import { createPathComponent } from "@react-leaflet/core";
import L from "leaflet";
import "leaflet.markercluster";
L.MarkerClusterGroup.include({
_flushLayerBuffer() {
this.eachLayer((layer:any)=>{
this.removeLayer(layer);
});
this.addLayers(this._layerBuffer);
this._layerBuffer = [];
},
addLayer(layer) {
if (this._layerBuffer.length === 0) {
setTimeout(this._flushLayerBuffer.bind(this), 50);
}
this._layerBuffer.push(layer);
},
});
L.MarkerClusterGroup.addInitHook(function () {
this._layerBuffer = [];
});
// eslint-disable-next-line no-unused-vars
function createMarkerCluster({ children: _c, ...props }, context) {
const clusterProps = {};
const clusterEvents = {};
// Splitting props and events to different objects
Object.entries(props).forEach(([propName, prop]) =>
propName.startsWith("on")
? (clusterEvents[propName] = prop)
: (clusterProps[propName] = prop),
);
const instance = new L.MarkerClusterGroup(clusterProps);
// Initializing event listeners
Object.entries(clusterEvents).forEach(([eventAsProp, callback]) => {
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`;
instance.on(clusterEvent, callback);
});
return {
instance,
context: {
...context,
layerContainer: instance,
},
};
}
const MarkerCluster = createPathComponent(createMarkerCluster);
export default MarkerCluster;