fix error in cluster
This commit is contained in:
@@ -192,9 +192,10 @@ export const MarkerCluster = () => {
|
||||
}[]
|
||||
>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const handleZoom = () => {
|
||||
// Compute clusters based on zoom and data using useMemo
|
||||
const clusters = useMemo(() => {
|
||||
const zoom = map.getZoom();
|
||||
if (zoom >= 8) return [];
|
||||
let newCluster: typeof cluster = [];
|
||||
aircrafts
|
||||
?.filter((a) => checkSimulatorConnected(a.lastHeartbeat))
|
||||
@@ -264,8 +265,6 @@ export const MarkerCluster = () => {
|
||||
const missionPos = c.missions.map((m) => [m.addressLat, m.addressLng]);
|
||||
const allPos = [...aircraftPos, ...missionPos];
|
||||
|
||||
// Calculate the average position of all markers in the cluster
|
||||
|
||||
const avgLat =
|
||||
allPos.reduce((sum, pos) => sum + pos[0]!, 0) / allPos.length;
|
||||
const avgLng =
|
||||
@@ -278,18 +277,27 @@ export const MarkerCluster = () => {
|
||||
};
|
||||
});
|
||||
|
||||
return clusterWithAvgPos;
|
||||
}, [aircrafts, filteredMissions, map]);
|
||||
|
||||
// Update clusters on zoom change
|
||||
useEffect(() => {
|
||||
const handleZoom = () => {
|
||||
const zoom = map.getZoom();
|
||||
if (zoom >= 8) {
|
||||
setCluster([]);
|
||||
} else {
|
||||
setCluster(clusterWithAvgPos);
|
||||
setCluster(clusters);
|
||||
}
|
||||
};
|
||||
handleZoom();
|
||||
map.on("zoomend", handleZoom);
|
||||
// Set initial clusters
|
||||
handleZoom();
|
||||
return () => {
|
||||
map.off("zoomend", handleZoom);
|
||||
};
|
||||
}, [map, aircrafts, missions, filteredMissions]);
|
||||
}, [map, clusters]);
|
||||
|
||||
return (
|
||||
<>
|
||||
{cluster.map((c, i) => (
|
||||
|
||||
Reference in New Issue
Block a user