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,56 +192,27 @@ 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();
let newCluster: typeof cluster = []; if (zoom >= 8) return [];
aircrafts let newCluster: typeof cluster = [];
?.filter((a) => checkSimulatorConnected(a.lastHeartbeat)) aircrafts
.forEach((aircraft) => { ?.filter((a) => checkSimulatorConnected(a.lastHeartbeat))
const lat = aircraft.posLat!; .forEach((aircraft) => {
const lng = aircraft.posLng!; const lat = aircraft.posLat!;
const lng = aircraft.posLng!;
const existingClusterIndex = newCluster.findIndex(
(c) => Math.abs(c.lat - lat) < 1 && Math.abs(c.lng - lng) < 1,
);
const existingCluster = newCluster[existingClusterIndex];
if (existingCluster) {
newCluster = [...newCluster].map((c, i) => {
if (i === existingClusterIndex) {
return {
...c,
aircrafts: [...c.aircrafts, aircraft],
};
}
return c;
});
} else {
newCluster = [
...newCluster,
{
aircrafts: [aircraft],
missions: [],
lat,
lng,
},
];
}
});
filteredMissions?.forEach((mission) => {
const lat = mission.addressLat;
const lng = mission.addressLng;
const existingClusterIndex = newCluster.findIndex( const existingClusterIndex = newCluster.findIndex(
(c) => Math.abs(c.lat - lat) < 1 && Math.abs(c.lng - lng) < 1, (c) => Math.abs(c.lat - lat) < 1 && Math.abs(c.lng - lng) < 1,
); );
const existingCluster = newCluster[existingClusterIndex]; const existingCluster = newCluster[existingClusterIndex];
if (existingCluster) { if (existingCluster) {
newCluster = [...newCluster].map((c, i) => { newCluster = [...newCluster].map((c, i) => {
if (i === existingClusterIndex) { if (i === existingClusterIndex) {
return { return {
...c, ...c,
missions: [...c.missions, mission], aircrafts: [...c.aircrafts, aircraft],
}; };
} }
return c; return c;
@@ -250,46 +221,83 @@ export const MarkerCluster = () => {
newCluster = [ newCluster = [
...newCluster, ...newCluster,
{ {
aircrafts: [], aircrafts: [aircraft],
missions: [mission], missions: [],
lat, lat,
lng, lng,
}, },
]; ];
} }
}); });
filteredMissions?.forEach((mission) => {
const lat = mission.addressLat;
const lng = mission.addressLng;
const existingClusterIndex = newCluster.findIndex(
(c) => Math.abs(c.lat - lat) < 1 && Math.abs(c.lng - lng) < 1,
);
const existingCluster = newCluster[existingClusterIndex];
const clusterWithAvgPos = newCluster.map((c) => { if (existingCluster) {
const aircraftPos = c.aircrafts.map((a) => [a.posLat, a.posLng]); newCluster = [...newCluster].map((c, i) => {
const missionPos = c.missions.map((m) => [m.addressLat, m.addressLng]); if (i === existingClusterIndex) {
const allPos = [...aircraftPos, ...missionPos]; return {
...c,
missions: [...c.missions, mission],
};
}
return c;
});
} else {
newCluster = [
...newCluster,
{
aircrafts: [],
missions: [mission],
lat,
lng,
},
];
}
});
// Calculate the average position of all markers in the cluster const clusterWithAvgPos = newCluster.map((c) => {
const aircraftPos = c.aircrafts.map((a) => [a.posLat, a.posLng]);
const missionPos = c.missions.map((m) => [m.addressLat, m.addressLng]);
const allPos = [...aircraftPos, ...missionPos];
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 =
allPos.reduce((sum, pos) => sum + pos[1]!, 0) / allPos.length; allPos.reduce((sum, pos) => sum + pos[1]!, 0) / allPos.length;
return { return {
...c, ...c,
lat: avgLat, lat: avgLat,
lng: avgLng, lng: avgLng,
}; };
}); });
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) => (