fix error in cluster

This commit is contained in:
nocnico
2025-05-22 01:48:18 +02:00
parent a8c27b6ac0
commit 0f04174516

View File

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