|
| 1 | +import { useDebounceFn } from '@vueuse/core' |
| 2 | + |
| 3 | +// Parse #@lat,lng,zoomz format from hash |
| 4 | +function parseMapHash(hash: string): { lat?: number, lng?: number, zoom?: number, bearing?: number, pitch?: number } { |
| 5 | + const match = hash.match(/^#@(-?\d+(?:\.\d*)?),(-?\d+(?:\.\d*)?),(\d+(?:\.\d*)?)z(?:,(\d+(?:\.\d*)?)b)?(?:,(\d+(?:\.\d*)?)p)?/) |
| 6 | + if (!match || !match[1] || !match[2] || !match[3]) |
| 7 | + return {} |
| 8 | + return { |
| 9 | + lat: Number.parseFloat(match[1]), |
| 10 | + lng: Number.parseFloat(match[2]), |
| 11 | + zoom: Number.parseFloat(match[3]), |
| 12 | + bearing: match[4] ? Number.parseFloat(match[4]) : undefined, |
| 13 | + pitch: match[5] ? Number.parseFloat(match[5]) : undefined, |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +// Build hash in #@lat,lng,zoomz format |
| 18 | +function buildMapHash(lat: number, lng: number, zoom: number, bearing?: number, pitch?: number): string { |
| 19 | + const latStr = lat.toFixed(5) |
| 20 | + const lngStr = lng.toFixed(5) |
| 21 | + const zoomStr = zoom.toFixed(2).replace(/\.?0+$/, '') |
| 22 | + let hash = `#@${latStr},${lngStr},${zoomStr}z` |
| 23 | + if (bearing && bearing !== 0) |
| 24 | + hash += `,${bearing.toFixed(0)}b` |
| 25 | + if (pitch && pitch !== 0) |
| 26 | + hash += `,${pitch.toFixed(0)}p` |
| 27 | + return hash |
| 28 | +} |
| 29 | + |
| 30 | +function useMapUrlBase() { |
| 31 | + const router = useRouter() |
| 32 | + const route = useRoute() |
| 33 | + const { viewCenter, zoom, bearing, pitch, isInitialized, mapInstance } = useMapControls() |
| 34 | + |
| 35 | + // Prevent feedback loop: URL change → map move → URL change |
| 36 | + const isUpdatingFromMap = ref(false) |
| 37 | + |
| 38 | + // Parse hash params |
| 39 | + const hashParams = computed(() => parseMapHash(route.hash)) |
| 40 | + const urlLat = computed(() => hashParams.value.lat) |
| 41 | + const urlLng = computed(() => hashParams.value.lng) |
| 42 | + const urlZoom = computed(() => hashParams.value.zoom) |
| 43 | + const urlBearing = computed(() => hashParams.value.bearing) |
| 44 | + const urlPitch = computed(() => hashParams.value.pitch) |
| 45 | + |
| 46 | + // Map → URL (debounced) |
| 47 | + const updateUrlFromMap = useDebounceFn(() => { |
| 48 | + if (!isInitialized.value) |
| 49 | + return |
| 50 | + isUpdatingFromMap.value = true |
| 51 | + |
| 52 | + const hash = buildMapHash( |
| 53 | + viewCenter.value.lat, |
| 54 | + viewCenter.value.lng, |
| 55 | + zoom.value, |
| 56 | + bearing.value, |
| 57 | + pitch.value, |
| 58 | + ) |
| 59 | + |
| 60 | + router.replace({ hash, query: route.query }).finally(() => { |
| 61 | + nextTick(() => { |
| 62 | + isUpdatingFromMap.value = false |
| 63 | + }) |
| 64 | + }) |
| 65 | + }, 300) |
| 66 | + |
| 67 | + // Watch map state → update URL |
| 68 | + watch([viewCenter, zoom, bearing, pitch], () => { |
| 69 | + if (isInitialized.value && mapInstance.value) |
| 70 | + updateUrlFromMap() |
| 71 | + }, { deep: true }) |
| 72 | + |
| 73 | + // URL → Map (external navigation / back-forward) |
| 74 | + watch([urlLat, urlLng, urlZoom, urlBearing, urlPitch], ([lat, lng, z, b, p]) => { |
| 75 | + if (isUpdatingFromMap.value) |
| 76 | + return |
| 77 | + if (!mapInstance.value || !isInitialized.value) |
| 78 | + return |
| 79 | + if (lat === undefined || lng === undefined) |
| 80 | + return |
| 81 | + |
| 82 | + const latDiff = Math.abs(viewCenter.value.lat - lat) |
| 83 | + const lngDiff = Math.abs(viewCenter.value.lng - lng) |
| 84 | + const posChanged = latDiff > 0.0001 || lngDiff > 0.0001 |
| 85 | + const zoomChanged = z !== undefined && Math.abs(zoom.value - z) > 0.5 |
| 86 | + const bearingChanged = b !== undefined && Math.abs(bearing.value - b) > 1 |
| 87 | + const pitchChanged = p !== undefined && Math.abs(pitch.value - p) > 1 |
| 88 | + |
| 89 | + if (posChanged || zoomChanged || bearingChanged || pitchChanged) { |
| 90 | + mapInstance.value.flyTo({ |
| 91 | + center: [lng, lat], |
| 92 | + zoom: z ?? zoom.value, |
| 93 | + bearing: b ?? bearing.value, |
| 94 | + pitch: p ?? pitch.value, |
| 95 | + duration: 1000, |
| 96 | + }) |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + return { urlLat, urlLng, urlZoom, urlBearing, urlPitch } |
| 101 | +} |
| 102 | + |
| 103 | +export const useMapUrl = createSharedComposable(useMapUrlBase) |
0 commit comments