diff --git a/resources/js/components/LeafletMap.vue b/resources/js/components/LeafletMap.vue index a657366..ae08835 100644 --- a/resources/js/components/LeafletMap.vue +++ b/resources/js/components/LeafletMap.vue @@ -1,38 +1,28 @@ @@ -87,13 +77,12 @@ function escHtml(str) { const props = defineProps({ stations: {type: Array, required: true}, - defaultOpen: {type: Boolean, default: false}, + isOpen: {type: Boolean, default: true}, radiusMiles: {type: Number, default: 10}, origin: {type: Object, default: null}, }) const mapContainer = ref(null) -const isOpen = ref(false) let mapInstance = null let markersLayer = null let userMarker = null @@ -102,8 +91,9 @@ function getZoomForRadius(radiusMiles) { if (radiusMiles <= 1) return 16 if (radiusMiles <= 2) return 15 if (radiusMiles <= 5) return 14 - if (radiusMiles <= 10) return 13 - if (radiusMiles <= 15) return 11 + if (radiusMiles <= 10) return 12 + if (radiusMiles <= 15) return 12 + if (radiusMiles <= 20) return 10 if (radiusMiles <= 25) return 10 if (radiusMiles <= 50) return 9 return 8 @@ -174,6 +164,10 @@ function initMap() { markersLayer = L.layerGroup().addTo(mapInstance) + mapInstance.on('zoomend', () => { + console.log('Map zoom:', mapInstance.getZoom()) + }) + locateUser() } @@ -238,47 +232,45 @@ function renderMarkers() { }) const zoom = getZoomForRadius(props.radiusMiles) + const center = props.origin?.lat != null && props.origin?.lng != null + ? [props.origin.lat, props.origin.lng] + : bounds[0] - if (bounds.length === 1) { - mapInstance.setView(bounds[0], zoom) - } else { - mapInstance.fitBounds(bounds, {padding: [40, 40], maxZoom: zoom}) - } + mapInstance.setView(center, zoom) } -async function toggleMap() { - isOpen.value = !isOpen.value - - if (isOpen.value) { - await nextTick() - initMap() - mapInstance.invalidateSize() - renderMarkers() - } -} - -onMounted(async () => { - if (props.defaultOpen) { - isOpen.value = true - await nextTick() - initMap() - mapInstance.invalidateSize() - renderMarkers() - } -}) - -watch(() => props.stations, () => { - if (isOpen.value) { - renderMarkers() - } -}) - -onUnmounted(() => { +function destroyMap() { if (mapInstance) { mapInstance.remove() mapInstance = null + markersLayer = null + userMarker = null + } +} + +async function openMap() { + await nextTick() + initMap() + mapInstance?.invalidateSize() + renderMarkers() +} + +onMounted(() => { + if (props.isOpen) openMap() +}) + +watch(() => props.isOpen, (open) => { + if (open) openMap() + else destroyMap() +}) + +watch(() => props.stations, () => { + if (props.isOpen) { + renderMarkers() } }) + +onUnmounted(destroyMap)