Fixed marker-cluster position
This commit is contained in:
@@ -224,7 +224,6 @@ const AircraftPopupContent = ({ aircraft }: { aircraft: Aircraft }) => {
|
||||
};
|
||||
|
||||
const AircraftMarker = ({ aircraft }: { aircraft: Aircraft }) => {
|
||||
const aircrafts = useAircraftsStore((state) => state.aircrafts);
|
||||
const [hideMarker, setHideMarker] = useState(false);
|
||||
const map = useMap();
|
||||
const markerRef = useRef<LMarker>(null);
|
||||
|
||||
@@ -216,36 +216,34 @@ export const MarkerCluster = () => {
|
||||
}
|
||||
});
|
||||
|
||||
setCluster((prev) => {
|
||||
return prev.map((c) => {
|
||||
const aircraftPos = c.aircrafts.map((a) => [
|
||||
a.location.lat,
|
||||
a.location.lng,
|
||||
]);
|
||||
const missionPos = c.missions.map((m) => [
|
||||
m.addressLat,
|
||||
m.addressLng,
|
||||
]);
|
||||
const allPos = [...aircraftPos, ...missionPos];
|
||||
const clusterWithAvgPos = newCluster.map((c) => {
|
||||
const aircraftPos = c.aircrafts.map((a) => [
|
||||
a.location.lat,
|
||||
a.location.lng,
|
||||
]);
|
||||
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 =
|
||||
allPos.reduce((sum, pos) => sum + pos[1]!, 0) / allPos.length;
|
||||
// Calculate the average position of all markers in the cluster
|
||||
|
||||
return {
|
||||
...c,
|
||||
lat: avgLat,
|
||||
lng: avgLng,
|
||||
};
|
||||
});
|
||||
const avgLat =
|
||||
allPos.reduce((sum, pos) => sum + pos[0]!, 0) / allPos.length;
|
||||
const avgLng =
|
||||
allPos.reduce((sum, pos) => sum + pos[1]!, 0) / allPos.length;
|
||||
|
||||
console.log(allPos, { avgLat, avgLng });
|
||||
|
||||
return {
|
||||
...c,
|
||||
lat: avgLat,
|
||||
lng: avgLng,
|
||||
};
|
||||
});
|
||||
|
||||
if (zoom >= 9) {
|
||||
setCluster([]);
|
||||
} else {
|
||||
setCluster(newCluster);
|
||||
setCluster(clusterWithAvgPos);
|
||||
}
|
||||
};
|
||||
handleZoom();
|
||||
|
||||
Reference in New Issue
Block a user