Skip to content

Commit 359a3ea

Browse files
committed
upgrade react-leaflet-markercluster to v4.2.1
1 parent b328cae commit 359a3ea

4 files changed

Lines changed: 141 additions & 83 deletions

File tree

package-lock.json

Lines changed: 80 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
],
3232
"dependencies": {
3333
"@apollo/client": "^3.5.4",
34-
"@changey/react-leaflet-markercluster": "^4.0.0-rc1",
3534
"@dnd-kit/core": "^6.3.1",
3635
"@dnd-kit/sortable": "^10.0.0",
3736
"@dnd-kit/utilities": "^3.2.2",
@@ -95,6 +94,7 @@
9594
"react-intl-formatted-duration": "^4.0.0",
9695
"react-leaflet": "^3.2.5",
9796
"react-leaflet-bing-v2": "^5.2.3",
97+
"react-leaflet-markercluster": "^4.2.1",
9898
"react-onclickoutside": "^6.6.3",
9999
"react-popper": "^2.2.4",
100100
"react-query": "^3.34.2",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { useEffect } from "react";
2+
import L from "leaflet";
3+
import { useMap } from "react-leaflet";
4+
import "leaflet.markercluster";
5+
6+
/**
7+
* A wrapper for leaflet.markercluster that works with react-leaflet v3 and React 17
8+
* This avoids the hooks compatibility issues with react-leaflet-markercluster v4.x
9+
*/
10+
const MarkerClusterGroupWrapper = ({ children, maxClusterRadius = 80, chunkedLoading = true }) => {
11+
const map = useMap();
12+
13+
useEffect(() => {
14+
const markerClusterGroup = L.markerClusterGroup({
15+
maxClusterRadius,
16+
chunkedLoading,
17+
spiderfyOnMaxZoom: true,
18+
showCoverageOnHover: false,
19+
});
20+
21+
const markers = [];
22+
const childrenArray = Array.isArray(children) ? children : [children];
23+
24+
childrenArray.forEach((child) => {
25+
if (child?.props?.position) {
26+
const marker = L.marker(child.props.position, {
27+
icon: child.props.icon,
28+
zIndexOffset: child.props.zIndexOffset,
29+
});
30+
31+
// Add event handlers
32+
if (child.props.eventHandlers) {
33+
Object.entries(child.props.eventHandlers).forEach(([event, handler]) => {
34+
marker.on(event, handler);
35+
});
36+
}
37+
38+
// Handle tooltip
39+
if (child.props.title) {
40+
marker.bindTooltip(child.props.title);
41+
}
42+
43+
markers.push(marker);
44+
}
45+
});
46+
47+
markerClusterGroup.addLayers(markers);
48+
map.addLayer(markerClusterGroup);
49+
50+
return () => map.removeLayer(markerClusterGroup);
51+
}, [map, children, maxClusterRadius, chunkedLoading]);
52+
53+
// This component doesn't render anything directly - it just adds markers to the map
54+
return null;
55+
};
56+
57+
export default MarkerClusterGroupWrapper;

src/components/TaskPane/TaskNearbyList/TaskNearbyMap.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import PropTypes from "prop-types";
33
import { Component, useEffect, useRef } from "react";
44
import { FormattedMessage, injectIntl } from "react-intl";
55
import "leaflet-vectoricon";
6-
import MarkerClusterGroup from "@changey/react-leaflet-markercluster/src/react-leaflet-markercluster";
6+
import "leaflet.markercluster/dist/MarkerCluster.css";
7+
import MarkerClusterGroup from "./MarkerClusterWrapper";
78
import _cloneDeep from "lodash/cloneDeep";
89
import _map from "lodash/map";
910
import { AttributionControl, MapContainer, Marker, Tooltip, useMap } from "react-leaflet";
@@ -276,7 +277,7 @@ export class TaskNearbyMap extends Component {
276277
}}
277278
/>
278279
{coloredMarkers.length > 0 && (
279-
<MarkerClusterGroup key={Date.now()} maxClusterRadius={5}>
280+
<MarkerClusterGroup maxClusterRadius={5} chunkedLoading={true}>
280281
{coloredMarkers}
281282
</MarkerClusterGroup>
282283
)}

0 commit comments

Comments
 (0)