From 5b41072c461fda48f0ec1af62e8b4193f97ad277 Mon Sep 17 00:00:00 2001 From: ISA Date: Mon, 15 Jul 2024 11:38:13 +0200 Subject: [PATCH] fix: Reinzoomen, Rauszoom und CenterHere in Contextmenu --- components/MapComponent.js | 87 ++++++++++++++++++++++++++++++++----- services/apiService.js | 2 +- utils/zoomAndCenterUtils.js | 3 ++ 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/components/MapComponent.js b/components/MapComponent.js index 9ea3aad44..5a9097623 100644 --- a/components/MapComponent.js +++ b/components/MapComponent.js @@ -232,7 +232,22 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { const closeVersionInfoModal = () => { setShowVersionInfoModal(false); }; + //---------------------------------------------------- + //---------------------------------------------------- + // Kontextmenü Callback für "Reinzoomen" + const zoomInCallback = (e, map) => { + zoomIn(e, map); + }; + // Kontextmenü Callback für "Rauszoomen" + const zoomOutCallback = (map) => { + zoomOut(map); + }; + + // Kontextmenü Callback für "Hier zentrieren" + const centerHereCallback = (e, map) => { + centerHere(e, map); + }; //---------------------------------------------------- //-----Kontextmenu---------------- // Funktion zum Anzeigen der Koordinaten @@ -287,11 +302,47 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { //-----Kontextmenu----ende------------ - const { addItemsToMapContextMenu } = useMapContextMenu( - map, - hasRights, - addStationCallback - ); + const addItemsToMapContextMenu = () => { + if (!menuItemAdded) { + map.contextmenu.addItem({ + text: "Koordinaten anzeigen", + icon: "img/not_listed_location.png", + callback: showCoordinates, + }); + + map.contextmenu.addItem({ separator: true }); // Divider + + map.contextmenu.addItem({ + text: "Reinzoomen", + icon: "img/zoom_in.png", + callback: (e) => zoomInCallback(e, map), + }); + + map.contextmenu.addItem({ + text: "Rauszoomen", + icon: "img/zoom_out.png", + callback: () => zoomOutCallback(map), + }); + + map.contextmenu.addItem({ + text: "Hier zentrieren", + icon: "img/center_focus.png", + callback: (e) => centerHereCallback(e, map), + }); + + map.contextmenu.addItem({ separator: true }); // Another Divider + + map.contextmenu.addItem({ + text: "POI hinzufügen", + icon: "img/add_station.png", + className: "background-red", + callback: (event) => addStationCallback(event, hasRights), + }); + + setMenuItemAdded(true); // Menüpunkt wurde hinzugefült, Zustand aktualisieren + } + }; + //------------------------------------------ */ const layerNames = { "GSM Modem": "GSMMODEM", @@ -502,9 +553,18 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { }, [hasRights]); // Dieser Effekt läuft jedes Mal, wenn sich `hasRights` ändert. // Überprüfen der Benutzerrechte beim Initialisieren der Komponente - useEffect(() => { + /* useEffect(() => { fetchUserRights(); - }, []); + }, []); */ + useEffect(() => { + if (serverURL) { + fetchUserRights(serverURL).then((rights) => { + setUserRights(rights); + setIsRightsLoaded(true); + setHasRights(rights && rights.includes(56)); // Prüfen, ob Benutzer die Rechte hat + }); + } + }, [serverURL]); // Läuft, wenn serverURL sich ändert // Anzeigen von Modals basierend auf Benutzerrechten useEffect(() => { @@ -697,7 +757,7 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { }, }, "-", // Divider - + /* { text: "Koordinaten anzeigen", icon: "img/not_listed_location.png", @@ -705,13 +765,17 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { }, "-", // Divider { text: "Reinzoomen", icon: "img/zoom_in.png", callback: zoomIn }, - { text: "Rauszoomen", icon: "img/zoom_out.png", callback: zoomOut }, + { + text: "Rauszoomen", + icon: "img/zoom_out.png", + callback: () => zoomOutCallback(map), + }, { text: "Hier zentrieren", icon: "img/center_focus.png", callback: (e) => centerHere(e, map), }, - "-", // Divider + "-", // Divider */ ], }); @@ -2188,6 +2252,9 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { map.flyTo([51.41321407879154, 7.739617925303934], 7); } }, [zoomTrigger, map]); + //--------------------------------------------------------- + //---------------------------------------------------------zoomen in kontextmenü + //--------------------------------------------------------- //--------------------------------------------------------- diff --git a/services/apiService.js b/services/apiService.js index ae88e8163..173910df8 100644 --- a/services/apiService.js +++ b/services/apiService.js @@ -82,7 +82,7 @@ export const checkInternet = () => { }; // ---------------------------------------------- -export const fetchUserRights = async () => { +export const fetchUserRights = async (serverURL) => { try { const response = await fetch( `${serverURL}/talas5/ClientData/WebserviceMap.asmx/GisSystemStatic?idMap=${c}&idUser=${user}` diff --git a/utils/zoomAndCenterUtils.js b/utils/zoomAndCenterUtils.js index cffcfa8f1..9bb773d7e 100644 --- a/utils/zoomAndCenterUtils.js +++ b/utils/zoomAndCenterUtils.js @@ -1,3 +1,4 @@ +// utils/zoomAndCenterUtils.js export const zoomIn = (e, map) => { if (!map) { console.error("map is not defined in zoomIn"); @@ -16,6 +17,8 @@ export const zoomOut = (map) => { const x = 51.41321407879154; const y = 7.739617925303934; const zoom = 7; + console.log("map"); + console.log(map); map.flyTo([x, y], zoom); localStorage.setItem("mapZoom", map.getZoom()); localStorage.setItem("mapCenter", JSON.stringify(map.getCenter()));