Moved the restoreMapSettings function from MapComponent.js to mapUtils.js for better modularity and reusability. This change also helps in maintaining cleaner code and improving readability.
80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
// /utils/mapUtils.js
|
|
import L from "leaflet";
|
|
|
|
export const redrawPolyline = (lineData, lineColors, tooltipContents, map) => {
|
|
if (!lineData || !lineColors || !tooltipContents || !map) {
|
|
console.error("Invalid parameters for redrawPolyline");
|
|
return;
|
|
}
|
|
|
|
if (!lineData.coordinates || !Array.isArray(lineData.coordinates)) {
|
|
console.error("Invalid coordinates in lineData");
|
|
return;
|
|
}
|
|
|
|
const color = lineColors[lineData.idModul] || "#000000";
|
|
const tooltipContent =
|
|
tooltipContents[lineData.idModul] || "Standard-Tooltip-Inhalt";
|
|
|
|
if (lineData.polyline) map.removeLayer(lineData.polyline);
|
|
|
|
lineData.polyline = L.polyline(lineData.coordinates, {
|
|
color: color,
|
|
}).addTo(map);
|
|
|
|
lineData.polyline.bindTooltip(tooltipContent, {
|
|
permanent: false,
|
|
direction: "auto",
|
|
});
|
|
|
|
lineData.polyline.on("mouseover", () => {
|
|
lineData.polyline.setStyle({ weight: 10 });
|
|
lineData.polyline.bringToFront();
|
|
});
|
|
|
|
lineData.polyline.on("mouseout", () => {
|
|
lineData.polyline.setStyle({ weight: 5 });
|
|
});
|
|
};
|
|
|
|
export const saveLineData = (lineData) => {
|
|
fetch("/api/talas_v5_DB/gisLines/updateLineCoordinates", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
idModul: lineData.idModul,
|
|
idLD: lineData.idLD,
|
|
newCoordinates: lineData.coordinates,
|
|
}),
|
|
})
|
|
.then((response) => {
|
|
if (!response.ok) {
|
|
throw new Error("Fehler beim Speichern der Linienänderungen");
|
|
}
|
|
return response.json();
|
|
})
|
|
.then((data) => {
|
|
console.log("Linienänderungen gespeichert:", data);
|
|
})
|
|
.catch((error) => {
|
|
console.error("Fehler beim Speichern der Linienänderungen:", error);
|
|
});
|
|
};
|
|
// Call this function on page load to restore zoom and center
|
|
export const restoreMapSettings = (map) => {
|
|
const savedZoom = localStorage.getItem("mapZoom");
|
|
const savedCenter = localStorage.getItem("mapCenter");
|
|
|
|
if (savedZoom && savedCenter) {
|
|
try {
|
|
const centerCoords = JSON.parse(savedCenter);
|
|
map.setView(centerCoords, parseInt(savedZoom));
|
|
} catch (e) {
|
|
console.error("Error parsing stored map center:", e);
|
|
map.setView([53.111111, 8.4625], 12); // Standardkoordinaten und -zoom
|
|
}
|
|
}
|
|
};
|