forked from hackforla/311-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkerSpiderfier.js
54 lines (49 loc) · 1.46 KB
/
MarkerSpiderfier.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
import React from 'react';
import { withLeaflet, MapLayer } from 'react-leaflet';
import { layerGroup, Marker } from 'leaflet';
import 'overlapping-marker-spiderfier-leaflet/dist/oms';
class MarkerSpiderfier extends MapLayer {
createLeafletElement({ leaflet }) {
const { map } = leaflet;
this.spiderfier = this.createOverlappingMarkerSpiderfier(map);
const el = layerGroup();
this.contextValue = { ...leaflet, layerContainer: el };
return el;
}
componentDidMount() {
super.componentDidMount();
this.leafletElement.eachLayer(layer => {
if (layer instanceof Marker) {
this.spiderfier.addMarker(layer);
}
});
}
componentDidUpdate() {
this.leafletElement.eachLayer(layer => {
if (layer instanceof Marker) {
this.spiderfier.addMarker(layer);
}
});
}
createOverlappingMarkerSpiderfier(map) {
const spiderfier = new window.OverlappingMarkerSpiderfier(map);
spiderfier.addListener('spiderfy', markers => {
markers.forEach(m => m.closePopup());
if (this.props.onSpiderfy) {
this.props.onSpiderfy(markers);
}
});
spiderfier.addListener('unspiderfy', markers => {
if (this.props.onUnspiderfy) {
this.props.onUnspiderfy(markers);
}
});
spiderfier.addListener('click', marker => {
if (this.props.onClick) {
this.props.onClick(marker);
}
});
return spiderfier;
}
}
export default withLeaflet(MarkerSpiderfier);