60 lines
1.9 KiB
JavaScript
60 lines
1.9 KiB
JavaScript
// /utils/markerUtils.js
|
|
import L from "leaflet";
|
|
import { toast } from "react-toastify";
|
|
import circleIcon from "@/components/gisPolylines/icons/CircleIcon";
|
|
import { store } from "../redux/store";
|
|
import { updatePolylineCoordinatesThunk } from "@/redux/thunks/database/polylines/updatePolylineCoordinatesThunk";
|
|
import { redrawPolyline } from "@/utils/mapUtils";
|
|
import { cleanupMarkers } from "@/utils/common/cleanupMarkers";
|
|
|
|
const savePolylineRedux = lineData => {
|
|
return store
|
|
.dispatch(
|
|
updatePolylineCoordinatesThunk({
|
|
idModul: lineData.idModul,
|
|
idLD: lineData.idLD,
|
|
newCoordinates: lineData.coordinates,
|
|
})
|
|
)
|
|
.unwrap()
|
|
.catch(error => {
|
|
console.error("Fehler beim Speichern der Linienkoordinaten:", error.message);
|
|
});
|
|
};
|
|
|
|
export const insertNewMarker = (closestPoints, newPoint, lineData, map) => {
|
|
const newMarker = L.marker(newPoint, {
|
|
icon: circleIcon,
|
|
draggable: true,
|
|
}).addTo(map);
|
|
|
|
lineData.coordinates.splice(closestPoints[2], 0, [newPoint.lat, newPoint.lng]);
|
|
savePolylineRedux(lineData); // ✅ Nutzung der Hilfsfunktion
|
|
redrawPolyline(lineData);
|
|
|
|
newMarker.on("dragend", () => {
|
|
const newCoords = newMarker.getLatLng();
|
|
const newCoordinates = [...lineData.coordinates];
|
|
newCoordinates[closestPoints[2]] = [newCoords.lat, newCoords.lng];
|
|
lineData.coordinates = newCoordinates;
|
|
|
|
redrawPolyline(lineData);
|
|
updateMarkerPosition(newMarker.getLatLng(), lineData, newMarker);
|
|
savePolylineRedux(lineData); // ✅ Wiederverwendung
|
|
});
|
|
};
|
|
|
|
export const removeMarker = (marker, lineData, currentZoom, currentCenter) => {
|
|
const index = lineData.coordinates.findIndex(coord =>
|
|
L.latLng(coord[0], coord[1]).equals(marker.getLatLng())
|
|
);
|
|
|
|
if (index !== -1) {
|
|
lineData.coordinates.splice(index, 1);
|
|
redrawPolyline(lineData);
|
|
cleanupMarkers([marker]);
|
|
savePolylineRedux(lineData); // ✅ Wiederverwendung
|
|
window.location.reload();
|
|
}
|
|
};
|