-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathreact-leaflet-markercluster.js
34 lines (27 loc) · 1.02 KB
/
react-leaflet-markercluster.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
import { createPathComponent } from '@react-leaflet/core';
import L from 'leaflet';
require('leaflet.markercluster');
const MarkerClusterGroup = createPathComponent(
({ children: _c, ...props }, ctx) => {
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)
);
// Creating markerClusterGroup Leaflet element
const markerClusterGroup = new L.markerClusterGroup(clusterProps);
// Initializing event listeners
Object.entries(clusterEvents).forEach(([eventAsProp, callback]) => {
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`;
markerClusterGroup.on(clusterEvent, callback);
});
return {
instance: markerClusterGroup,
context: { ...ctx, layerContainer: markerClusterGroup },
};
}
);
export default MarkerClusterGroup;