Files
nodeMap/utils/mapUtils.js

77 lines
2.3 KiB
JavaScript

// /utils/mapUtils.js
import L from "leaflet";
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
}
}
};
// Now update checkOverlappingMarkers to check if oms is initialized
export const checkOverlappingMarkers = (map, markers, plusIcon) => {
// Ensure markers is always an array
if (!Array.isArray(markers)) {
console.error("The `markers` argument is not an array:", markers);
return;
}
const overlappingGroups = {};
// Group markers by coordinates as strings
markers.forEach((marker) => {
const latlngStr = marker.getLatLng().toString();
if (overlappingGroups[latlngStr]) {
overlappingGroups[latlngStr].push(marker);
} else {
overlappingGroups[latlngStr] = [marker];
}
});
// Add plus markers at coordinates where overlaps occur
for (const coords in overlappingGroups) {
if (overlappingGroups[coords].length > 1) {
const latLng = L.latLng(coords.match(/[-.\d]+/g).map(Number));
const plusMarker = L.marker(latLng, { icon: plusIcon });
plusMarker.addTo(map);
console.log("Adding plus icon marker at", latLng);
}
}
console.log("overlappingGroups", overlappingGroups);
};