feat: paste centerHere function into zoomAndCenterUtil.js with error

This commit is contained in:
ISA
2024-07-12 07:03:37 +02:00
parent 91923ef8b4
commit 042d086a49
2 changed files with 60 additions and 9 deletions

View File

@@ -79,7 +79,7 @@ import { MAP_VERSION } from "../config/settings";
import * as layers from "../config/layers.js"; import * as layers from "../config/layers.js";
import useMapContextMenu from "./useMapContextMenu.js"; import useMapContextMenu from "./useMapContextMenu.js";
import { openInSameWindow } from "../utils/openInSameWindow"; import { openInSameWindow } from "../utils/openInSameWindow";
import { zoomIn, zoomOut } from "../utils/zoomUtils"; import { zoomIn, zoomOut, centerHere } from "../utils/zoomAndCenterUtils.js";
//--------------------------------------------------------------------- //---------------------------------------------------------------------
//-------------------- MapComponent ----------------------------------- //-------------------- MapComponent -----------------------------------
const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
@@ -235,10 +235,6 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
//---------------------------------------------------- //----------------------------------------------------
//-----Kontextmenu---------------- //-----Kontextmenu----------------
const centerHere = (e) => {
initMap.panTo(e.latlng);
};
// Funktion zum Anzeigen der Koordinaten // Funktion zum Anzeigen der Koordinaten
const showCoordinates = (e) => { const showCoordinates = (e) => {
alert( alert(
@@ -708,14 +704,14 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
callback: showCoordinates, callback: showCoordinates,
}, },
"-", // Divider "-", // Divider
{ text: "Reinzoomen", icon: "img/zoom_in.png", callback: zoomIn }, /* { 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: zoomOut },
{ {
text: "Hier zentrieren", text: "Hier zentrieren",
icon: "img/center_focus.png", icon: "img/center_focus.png",
callback: centerHere, callback: (e) => centerHere(e, map),
}, },
"-", // Divider "-", */ // Divider
], ],
}); });
@@ -750,6 +746,45 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
//console.log("trigger in MapComponent.js:", poiReadTrigger); //console.log("trigger in MapComponent.js:", poiReadTrigger);
}, [mapRef, map, poiReadTrigger, contextMenuItems]); // Prüfe die Abhängigkeiten sorgfältig }, [mapRef, map, poiReadTrigger, contextMenuItems]); // Prüfe die Abhängigkeiten sorgfältig
useEffect(() => {
if (map) {
map.on("contextmenu", (e) => {
const contextMenuItems = [
{
text: "Reinzoomen",
icon: "img/zoom_in.png",
callback: (e) => zoomIn(e, map),
},
{
text: "Rauszoomen",
icon: "img/zoom_out.png",
callback: () => zoomOut(map),
},
{
text: "Hier zentrieren",
icon: "img/center_focus.png",
callback: (e) => centerHere(e, map),
},
"-",
];
L.DomEvent.stopPropagation(e);
const contextMenu = L.popup()
.setLatLng(e.latlng)
.setContent(
'<div class="leaflet-popup-content-wrapper leaflet-popup-content"><div class="leaflet-popup-content" style="max-width: 300px;">' +
contextMenuItems
.map(
(item) =>
`<a href="#" onclick="(${item.callback.toString()})()">${item.text}</a>`
)
.join("<br>") +
"</div></div>"
)
.openOn(map);
});
}
}, [map]);
// poiTyp Daten hinzufügen // poiTyp Daten hinzufügen
//------------------------------------------ //------------------------------------------
// Funktion zum Abrufen der poiTyp Daten // Funktion zum Abrufen der poiTyp Daten

View File

@@ -1,11 +1,19 @@
// utils/zoomUtils.js // utils/zoomAndCenterUtils.js
export const zoomIn = (e, map) => { export const zoomIn = (e, map) => {
if (!map) {
console.error("map is not defined in zoomIn");
return;
}
map.flyTo(e.latlng, 12); map.flyTo(e.latlng, 12);
localStorage.setItem("mapZoom", map.getZoom()); localStorage.setItem("mapZoom", map.getZoom());
localStorage.setItem("mapCenter", JSON.stringify(map.getCenter())); localStorage.setItem("mapCenter", JSON.stringify(map.getCenter()));
}; };
export const zoomOut = (map) => { export const zoomOut = (map) => {
if (!map) {
console.error("map is not defined in zoomOut");
return;
}
const x = 51.41321407879154; const x = 51.41321407879154;
const y = 7.739617925303934; const y = 7.739617925303934;
const zoom = 7; const zoom = 7;
@@ -13,3 +21,11 @@ export const zoomOut = (map) => {
localStorage.setItem("mapZoom", map.getZoom()); localStorage.setItem("mapZoom", map.getZoom());
localStorage.setItem("mapCenter", JSON.stringify(map.getCenter())); localStorage.setItem("mapCenter", JSON.stringify(map.getCenter()));
}; };
export const centerHere = (e, map) => {
if (!map) {
console.error("map is not defined in centerHere");
return;
}
map.panTo(e.latlng);
};