119 lines
3.8 KiB
JavaScript
119 lines
3.8 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
|
|
// Globales Array, um Plus-Icons zu speichern
|
|
let plusMarkers = [];
|
|
|
|
export const checkOverlappingMarkers = (map, markers, plusIcon) => {
|
|
const overlappingGroups = {};
|
|
|
|
// Gruppiere Marker basierend auf ihrer Position
|
|
markers.forEach((marker) => {
|
|
if (map.hasLayer(marker)) {
|
|
// Überprüfen, ob der Marker sichtbar ist
|
|
const latlngStr = marker.getLatLng().toString();
|
|
if (overlappingGroups[latlngStr]) {
|
|
overlappingGroups[latlngStr].push(marker);
|
|
} else {
|
|
overlappingGroups[latlngStr] = [marker];
|
|
}
|
|
}
|
|
});
|
|
|
|
// Entferne alte Plus-Icons
|
|
plusMarkers.forEach((plusMarker) => map.removeLayer(plusMarker));
|
|
plusMarkers = [];
|
|
|
|
// Füge Plus-Icons für überlappende Marker hinzu
|
|
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 }).addTo(map);
|
|
plusMarkers.push(plusMarker); // Speichere das Plus-Icon
|
|
|
|
plusMarker.on("click", (e) => {
|
|
const clickedLatLng = e.latlng;
|
|
|
|
// Finde nahegelegene Marker (50 Pixel als Beispielradius)
|
|
const nearbyMarkers = markers.filter((marker) => map.distance(marker.getLatLng(), clickedLatLng) < 50);
|
|
|
|
console.log("Nearby Markers for Plus Icon:", nearbyMarkers);
|
|
|
|
if (nearbyMarkers.length > 0) {
|
|
// Simuliere einen Klick nur auf den ersten Marker
|
|
nearbyMarkers[0].fire("click");
|
|
console.log("Clicked on nearby Marker[0]:", nearbyMarkers[0]);
|
|
} else {
|
|
console.log("Keine Marker gefunden.");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// Entferne alle Plus-Icons, wenn keine Marker sichtbar sind
|
|
if (Object.keys(overlappingGroups).length === 0) {
|
|
plusMarkers.forEach((plusMarker) => map.removeLayer(plusMarker));
|
|
plusMarkers = [];
|
|
}
|
|
};
|
|
|
|
export const handlePlusIconClick = (map, markers, oms, clickedLatLng) => {
|
|
// Debugging-Ausgabe
|
|
console.log("Plus-Icon Position:", clickedLatLng);
|
|
|
|
// Finde Marker in der Nähe der Klickposition
|
|
const nearbyMarkers = markers.filter((marker) => map.distance(marker.getLatLng(), clickedLatLng) < 50);
|
|
|
|
// Debugging-Ausgabe
|
|
console.log("Gefundene Marker in der Nähe:", nearbyMarkers);
|
|
|
|
if (oms && nearbyMarkers.length > 0) {
|
|
// Spiderfy die gefundenen Marker
|
|
oms.spiderfy(nearbyMarkers);
|
|
} else {
|
|
console.log("Keine überlappenden Marker gefunden.");
|
|
}
|
|
};
|