Popups per Mouse-Over statt per Klick zu öffnen

This commit is contained in:
ISA
2024-04-17 06:58:17 +02:00
parent 48c3b12952
commit 250ce923f0
2 changed files with 50 additions and 27 deletions

View File

@@ -120,13 +120,32 @@ const MapComponent = ({ locations, onLocationUpdate }) => {
}), }),
}).addTo(map); }).addTo(map);
// Popup beim Überfahren mit der Maus öffnen
marker.on("mouseover", function (e) {
this.openPopup();
});
// Popup schließen, wenn die Maus den Marker verlässt
marker.on("mouseout", function (e) {
this.closePopup();
});
// Ein Popup mit Informationen zur Station hinzufügen // Ein Popup mit Informationen zur Station hinzufügen
marker marker
.bindPopup(`<b>${station.LD_Name}</b><br>${station.Device}`) .bindPopup(`<b>${station.LD_Name}</b><br>${station.Device}`)
.openPopup(); .openPopup();
marker.bindTooltip(station.LD_Name, { marker.bindTooltip(
permanent: false, `<div class="tooltip-content">${station.LD_Name}</div>`,
direction: "top", { permanent: false, direction: "top" }
);
// Zugriff auf das Tooltip-Element und Anwendung von Stilen
marker.on("mouseover", () => {
document.querySelector(".tooltip-content").style.backgroundColor =
"#ffffff";
document.querySelector(".tooltip-content").style.padding = "0.5rem";
document.querySelector(".tooltip-content").style.borderRadius =
"0.25rem";
document.querySelector(".tooltip-content").style.border =
"1px solid #d1d5db";
}); });
}); });
} }
@@ -405,10 +424,10 @@ const MapComponent = ({ locations, onLocationUpdate }) => {
marker.bindPopup( marker.bindPopup(
`<b>${location.description || "Unbekannt"}</b><br>Type: ${location.idPoiTyp || "N/A"}<br>Lat: ${latitude.toFixed(5)}, Lng: ${longitude.toFixed(5)}` `<b>${location.description || "Unbekannt"}</b><br>Type: ${location.idPoiTyp || "N/A"}<br>Lat: ${latitude.toFixed(5)}, Lng: ${longitude.toFixed(5)}`
); );
marker.bindTooltip(location.description, { marker.bindTooltip(
permanent: false, `<div class="bg-white text-black p-2 border border-gray-300 rounded shadow">${location.description}</div>`,
direction: "top", { permanent: false, direction: "top" }
}); );
marker.on("dragend", function (e) { marker.on("dragend", function (e) {
const newLat = e.target.getLatLng().lat; const newLat = e.target.getLatLng().lat;
@@ -666,11 +685,7 @@ const MapComponent = ({ locations, onLocationUpdate }) => {
//Informationen in Tooltips einfügen //Informationen in Tooltips einfügen
// Marker hinzufügen für GisStationsStaticDistrict // Marker hinzufügen für GisStationsStaticDistrict
useEffect(() => { useEffect(() => {
if ( if (map && GisStationsStaticDistrict.length > 0) {
map &&
GisStationsStaticDistrict.length > 0 &&
GisStationsStatusDistrict.length > 0
) {
// Zuerst alte Marker entfernen // Zuerst alte Marker entfernen
map.eachLayer((layer) => { map.eachLayer((layer) => {
if (layer instanceof L.Marker) { if (layer instanceof L.Marker) {
@@ -680,12 +695,6 @@ const MapComponent = ({ locations, onLocationUpdate }) => {
// Neue Marker für jede Station hinzufügen // Neue Marker für jede Station hinzufügen
GisStationsStaticDistrict.forEach((station) => { GisStationsStaticDistrict.forEach((station) => {
// Find corresponding status message
const status = GisStationsStatusDistrict.find(
(status) => status.IdLD === station.IdLD
);
const message = status ? status.Me : "Keine Statusmeldung verfügbar";
const marker = L.marker([station.X, station.Y], { const marker = L.marker([station.X, station.Y], {
icon: L.icon({ icon: L.icon({
iconUrl: getIconPath(station.Icon), iconUrl: getIconPath(station.Icon),
@@ -696,15 +705,25 @@ const MapComponent = ({ locations, onLocationUpdate }) => {
}), }),
}).addTo(map); }).addTo(map);
// Ein Popup mit Informationen zur Station und Statusmeldung hinzufügen // Popup beim Überfahren mit der Maus öffnen
marker.on("mouseover", function (e) {
this.openPopup();
});
// Popup schließen, wenn die Maus den Marker verlässt
marker.on("mouseout", function (e) {
this.closePopup();
});
// Ein Popup mit Informationen zur Station hinzufügen
marker marker
.bindPopup( .bindPopup(`<b>${station.LD_Name}</b><br>${station.Device}`)
`<b>${station.LD_Name}</b><br>${station.Device}<br><b>Status:</b> ${message}` .openPopup();
)
.bindTooltip(`${station.LD_Name} - ${message}`, { // Diese Zeile entfernt das automatische Tooltip
permanent: false, // marker.bindTooltip(station.LD_Name, {
direction: "top", // permanent: false,
}); // direction: "top",
// });
}); });
} }
}, [map, GisStationsStaticDistrict, GisStationsStatusDistrict]); // Include GisStationsStatusDistrict in dependencies }, [map, GisStationsStaticDistrict, GisStationsStatusDistrict]); // Include GisStationsStatusDistrict in dependencies

View File

@@ -1,5 +1,9 @@
/* styles/global.css */
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
@import 'leaflet/dist/leaflet.css'; @import "leaflet/dist/leaflet.css";
.tooltip-tailwind .leaflet-tooltip {
@apply bg-white text-black p-2 border border-gray-300 rounded shadow !important;
}