Files
nodeMap/hooks/layers/useGmaMarkersLayer.js

242 lines
7.5 KiB
JavaScript

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(
`
<div class="p-0 rounded-lg bg-white bg-opacity-90 tooltip-content">
<div class="font-bold text-sm text-black">
<span>${area_name}</span>
</div>
<div class="font-bold text-xxs text-blue-700">
<span>LT : ${lt} °C</span>
</div>
<div class="font-bold text-xxs text-red-700">
<span>FBT : ${fbt} °C</span>
</div>
<div class="font-bold text-xxs text-yellow-500">
<span>GT : ${gt} °C</span>
</div>
<div class="font-bold text-xxs text-green-700">
<span>RLF : ${rlf} %</span>
</div>
</div>
`,
{
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;