import React, { useEffect, useRef, useState } from "react"; import L from "leaflet"; import "leaflet/dist/leaflet.css"; import "leaflet-contextmenu/dist/leaflet.contextmenu.css"; import "leaflet-contextmenu"; const MapComponent = () => { const mapRef = useRef(null); const [map, setMap] = useState(null); let initialMap = []; useEffect(() => { if (mapRef.current && !map) { // Initialisiere die Karte ohne die Standard-Zoomsteuerung initialMap = L.map(mapRef.current, { center: [53.111111, 8.4625], zoom: 10, zoomControl: false, // Deaktiviere die Standard-Zoomsteuerung contextmenu: true, contextmenuItems: [ { text: "Station hinzufügen", callback: showAddStationPopup }, { text: "Station öffnen (Tab)", icon: "img/screen_new.png", callback: newLink, }, { text: "Station öffnen", icon: "img/screen_same.png", callback: sameLink, }, { text: "Koordinaten", icon: "img/screen_same.png", callback: lata, }, "-", // Divider { text: "Reinzoomen", callback: zoomIn }, { text: "Rauszoomen", callback: zoomOut }, { text: "Hier zentrieren", callback: centerHere }, ], }); console.log(initialMap); L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { attribution: '© OpenStreetMap contributors', }).addTo(initialMap); setMap(initialMap); } }, [mapRef, map]); console.log(map); //-----Kontextmenu---------------- const newLink = (e) => { try { if (!e.relatedTarget || !e.relatedTarget.options) { throw new Error("relatedTarget or options not defined"); } alert("Neues Fenster: " + e.relatedTarget.options.test); window .open(`../devices/${e.relatedTarget.options.test}`, "_blank") .focus(); } catch (error) { console.error("Failed in newLink function:", error); } }; const sameLink = (e) => { alert(e.relatedTarget.options.test); window .open("../devices/" + e.relatedTarget.options.test, "_parent") .focus(); }; const lata = (e) => { alert("Breitengrad: " + e.latlng.lat); }; const zoomIn = (e) => { initialMap.flyTo(e.latlng, 12); }; const zoomOut = (e) => { fly(); }; const centerHere = (e) => { initialMap.panTo(e.latlng); }; const showCoordinates = (e) => { alert("Breitengrad: " + e.latlng.lat + "\nLängengrad: " + e.latlng.lng); }; const showData = (e) => { console.log(e); }; const showTalas = (e) => { map.addLayer(TALAS); loadData(); }; const hideTalas = (e) => { map.removeLayer(TALAS); loadData(); }; const showGSM = (e) => { map.addLayer(GMA); loadData(); }; const hideGSM = (e) => { map.removeLayer(GMA); loadData(); }; //-----Kontextmenu----ende------------ const showAddStationPopup = (e) => { const popupContent = L.DomUtil.create("form"); popupContent.innerHTML = `