Fix duplicate handlers for map click setting and update map to only zoom to markers once

This commit is contained in:
Andrew Kingston 2022-03-15 13:54:32 +00:00
parent 9e96090333
commit 3fb71f91ef
1 changed files with 14 additions and 9 deletions

View File

@ -37,6 +37,7 @@
let mapInstance let mapInstance
let mapMarkerGroup = new L.FeatureGroup() let mapMarkerGroup = new L.FeatureGroup()
let mapMarkers = [] let mapMarkers = []
let loaded = false
let minZoomLevel = 0 let minZoomLevel = 0
let maxZoomLevel = 18 let maxZoomLevel = 18
@ -60,20 +61,13 @@
titleKey, titleKey,
onMarkerClick onMarkerClick
) )
$: if (typeof mapInstance === "object" && mapMarkers.length > 0) { $: if (!loaded && typeof mapInstance === "object" && mapMarkers.length > 0) {
loaded = true
mapInstance.setZoom(0) mapInstance.setZoom(0)
mapInstance.fitBounds(mapMarkerGroup.getBounds(), { mapInstance.fitBounds(mapMarkerGroup.getBounds(), {
paddingTopLeft: [0, 24], paddingTopLeft: [0, 24],
}) })
} }
$: mapInstance?.on("click", e => {
if (onMapClick) {
onMapClick({
lat: e.latlng.lat,
lng: e.latlng.lng,
})
}
})
const updateMapDimensions = mapInstance => { const updateMapDimensions = mapInstance => {
if (typeof mapInstance !== "object") { if (typeof mapInstance !== "object") {
@ -277,6 +271,17 @@
//Initialise the map //Initialise the map
mapInstance.setView(initCoords, adjustedZoomLevel) 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 = () => { const mapAction = () => {