From b3840f0ae0a902dc284e179c3f0497e21fd0e362 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 15 Mar 2022 13:54:32 +0000 Subject: [PATCH] Fix duplicate handlers for map click setting and update map to only zoom to markers once --- .../app/embedded-map/EmbeddedMap.svelte | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/client/src/components/app/embedded-map/EmbeddedMap.svelte b/packages/client/src/components/app/embedded-map/EmbeddedMap.svelte index 1746a79d95..d71873c0f3 100644 --- a/packages/client/src/components/app/embedded-map/EmbeddedMap.svelte +++ b/packages/client/src/components/app/embedded-map/EmbeddedMap.svelte @@ -37,6 +37,7 @@ let mapInstance let mapMarkerGroup = new L.FeatureGroup() let mapMarkers = [] + let loaded = false let minZoomLevel = 0 let maxZoomLevel = 18 @@ -60,20 +61,13 @@ titleKey, onMarkerClick ) - $: if (typeof mapInstance === "object" && mapMarkers.length > 0) { + $: if (!loaded && typeof mapInstance === "object" && mapMarkers.length > 0) { + loaded = true mapInstance.setZoom(0) mapInstance.fitBounds(mapMarkerGroup.getBounds(), { paddingTopLeft: [0, 24], }) } - $: mapInstance?.on("click", e => { - if (onMapClick) { - onMapClick({ - lat: e.latlng.lat, - lng: e.latlng.lng, - }) - } - }) const updateMapDimensions = mapInstance => { if (typeof mapInstance !== "object") { @@ -277,6 +271,17 @@ //Initialise the map mapInstance.setView(initCoords, adjustedZoomLevel) + + // Add click handler + mapInstance?.on("click", e => { + console.log("map clicked!") + if (onMapClick) { + onMapClick({ + lat: e.latlng.lat, + lng: e.latlng.lng, + }) + } + }) } const mapAction = () => {