Skip to content

Commit 7b460c1

Browse files
migurskiclaude
andcommitted
Fix hash position being overridden by geolocation or applyBaseStyle
Parse the URL hash before constructing the OL View so the initial center/zoom are set correctly from the start. This prevents applyBaseStyle (which saves and restores view state) from firing moveend and overwriting the hash before useMapHash wires up. Export parseMapHash from the composable and call it at the top of onMounted; skip geolocation only when a hash was present at that moment. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 159bc4b commit 7b460c1

2 files changed

Lines changed: 7 additions & 15 deletions

File tree

site/src/components/MapContainer.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import { apply } from 'ol-mapbox-style'
7070
import PoiPopup from './PoiPopup.vue'
7171
import ConfidenceLegend from './ConfidenceLegend.vue'
7272
import { useGeolocation } from '../composables/useGeolocation.js'
73-
import { useMapHash } from '../composables/useMapHash.js'
73+
import { useMapHash, parseMapHash } from '../composables/useMapHash.js'
7474
import {
7575
getOsmLayer,
7676
updateOsmFilters,
@@ -120,9 +120,10 @@ function getDataLayers() {
120120
}
121121
122122
onMounted(async () => {
123+
const hashState = parseMapHash()
123124
const view = new View({
124-
center: fromLonLat(INITIAL_CENTER),
125-
zoom: INITIAL_ZOOM,
125+
center: hashState ? hashState.center : fromLonLat(INITIAL_CENTER),
126+
zoom: hashState ? hashState.zoom : INITIAL_ZOOM,
126127
minZoom: MIN_ZOOM,
127128
})
128129
@@ -162,8 +163,8 @@ onMounted(async () => {
162163
updateOvertureFilters(props.overtureFilters)
163164
updateConflatedFilters(props.conflatedFilters)
164165
165-
const { hasHash } = useMapHash(olMap)
166-
if (!hasHash) handleGeolocate()
166+
useMapHash(olMap)
167+
if (!hashState) handleGeolocate()
167168
})
168169
169170
onBeforeUnmount(() => {

site/src/composables/useMapHash.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fromLonLat, toLonLat } from 'ol/proj'
22

33
const HASH_RE = /^#(\d+(?:\.\d+)?)\/(-?\d+(?:\.\d+)?)\/(-?\d+(?:\.\d+)?)$/
44

5-
function readHash() {
5+
export function parseMapHash() {
66
const m = HASH_RE.exec(window.location.hash)
77
if (!m) return null
88
return {
@@ -20,14 +20,5 @@ function writeHash(view) {
2020
}
2121

2222
export function useMapHash(map) {
23-
const initial = readHash()
24-
25-
if (initial) {
26-
map.getView().setCenter(initial.center)
27-
map.getView().setZoom(initial.zoom)
28-
}
29-
3023
map.on('moveend', () => writeHash(map.getView()))
31-
32-
return { hasHash: !!initial }
3324
}

0 commit comments

Comments
 (0)