import { useEffect } from "react"; import { addContextMenuToMarker } from "../../utils/addContextMenuToMarker"; const useGmaMarkersLayer = (map, markers, GisStationsMeasurements, GMA, oms, isVisible) => { const zoomIn = (map, latlng) => { if (!map) { console.error("map is not defined in zoomIn"); return; } const currentZoom = map.getZoom(); if (currentZoom < 14) { map.flyTo(latlng, 14); localStorage.setItem("mapZoom", 14); localStorage.setItem("mapCenter", JSON.stringify(map.getCenter())); } }; const zoomOut = (map) => { if (!map) { console.error("map is not defined in zoomOut"); return; } const currentZoom = map.getZoom(); if (currentZoom > 7) { const x = 51.41321407879154; const y = 7.739617925303934; const zoom = 7; map.flyTo([x, y], zoom); localStorage.setItem("mapZoom", zoom); localStorage.setItem("mapCenter", JSON.stringify(map.getCenter())); } }; const centerHere = (e, map) => { if (!map) { console.error("map is not defined in centerHere"); return; } map.panTo(e.latlng); localStorage.setItem("mapZoom", map.getZoom()); localStorage.setItem("mapCenter", JSON.stringify(map.getCenter())); }; useEffect(() => { if (!map || !isVisible) return; // Entferne alte Marker GMA.clearLayers(); markers.forEach((marker) => { const relevantMeasurements = GisStationsMeasurements.filter((m) => m.Area_Name === marker.options.areaName); let measurements = {}; let area_name = marker.options.areaName; relevantMeasurements.forEach((m) => { measurements[m.Na] = m.Val; }); const lt = measurements["LT"] || "-"; const fbt = measurements["FBT"] || "-"; const gt = measurements["GT"] || "-"; const rlf = measurements["RLF"] || "-"; marker.bindTooltip( `
${area_name}
LT : ${lt} °C
FBT : ${fbt} °C
GT : ${gt} °C
RLF : ${rlf} %
`, { permanent: true, direction: "auto", offset: [60, 0], interactive: true, } ); let currentMouseLatLng = null; const mouseMoveHandler = (event) => { currentMouseLatLng = event.latlng; }; // Mousemove-Listener hinzufügen, um die aktuellen Koordinaten zu speichern map.on("mousemove", mouseMoveHandler); marker.on("tooltipopen", (e) => { const tooltipElement = e.tooltip._contentNode; if (tooltipElement) { tooltipElement.addEventListener("contextmenu", (event) => { event.preventDefault(); console.log("Rechtsklick auf Tooltip erkannt"); // Kombiniere die Kontextmenü-Items const combinedContextMenuItems = [ { text: "Station öffnen (Tab)", icon: "/img/screen_new.png", callback: () => { if (marker.options.link) { const fullUrl = `${marker.options.link}`; window.open(fullUrl, "_blank"); console.log("Link in neuem Tab geöffnet:", fullUrl); } else { console.error("Kein Link für Tooltip vorhanden."); } }, }, { separator: true }, { text: "Koordinaten anzeigen", icon: "/img/not_listed_location.png", callback: () => { if (currentMouseLatLng) { alert( `Breitengrad: ${currentMouseLatLng.lat.toFixed(5)}\nLängengrad: ${currentMouseLatLng.lng.toFixed(5)}` ); } else { console.error("Keine gültigen Koordinaten erkannt."); } }, }, { separator: true }, { text: "Reinzoomen", icon: "img/zoom_in.png", callback: () => { const latlng = marker.getLatLng(); zoomIn(map, latlng); }, }, { text: "Rauszoomen", icon: "img/zoom_out.png", callback: () => zoomOut(map), }, { text: "Hier zentrieren", icon: "img/center_focus.png", callback: () => { const latlng = marker.getLatLng(); centerHere({ latlng }, map); }, }, ]; // Benutzerdefiniertes Kontextmenü anzeigen const menu = document.createElement("div"); menu.className = "custom-context-menu"; menu.style.position = "absolute"; menu.style.left = `${event.clientX}px`; menu.style.top = `${event.clientY}px`; menu.style.backgroundColor = "#fff"; menu.style.border = "1px solid #ccc"; menu.style.padding = "4px"; menu.style.zIndex = "1000"; combinedContextMenuItems.forEach((item) => { if (item.separator) { const separator = document.createElement("hr"); menu.appendChild(separator); } else { const menuItem = document.createElement("div"); menuItem.style.display = "flex"; menuItem.style.alignItems = "center"; menuItem.style.cursor = "pointer"; if (item.icon) { const icon = document.createElement("img"); icon.src = item.icon; icon.alt = "Icon"; icon.style.width = "16px"; icon.style.height = "16px"; icon.style.marginRight = "8px"; menuItem.appendChild(icon); } const text = document.createElement("span"); text.textContent = item.text; menuItem.appendChild(text); menuItem.onclick = item.callback; menu.appendChild(menuItem); } }); document.body.appendChild(menu); const handleClickOutside = () => { if (document.body.contains(menu)) { document.body.removeChild(menu); document.removeEventListener("click", handleClickOutside); } }; document.addEventListener("click", handleClickOutside); }); } }); marker.on("tooltipclose", () => { map.off("mousemove", mouseMoveHandler); }); addContextMenuToMarker(marker); GMA.addLayer(marker); oms.addMarker(marker); }); map.addLayer(GMA); return () => { GMA.clearLayers(); map.removeLayer(GMA); }; }, [map, markers, GisStationsMeasurements, GMA, oms, isVisible]); }; export default useGmaMarkersLayer;