mosaicmap/hooks/use-map-location.ts
2025-07-17 22:57:07 +08:00

52 lines
1.2 KiB
TypeScript

import { useState, useCallback } from 'react'
import { useMap } from '@/app/map-context'
const LOCATIONS = {
singapore: {
center: [103.851959, 1.290270] as [number, number],
zoom: 11
},
malaysia: {
center: [101.686852, 3.139003] as [number, number],
zoom: 8
},
// China Middle Point
china: {
center: [104.5, 35.5] as [number, number],
zoom: 4
}
} as const
export type LocationKey = keyof typeof LOCATIONS
export function useMapLocation() {
const [currentLocation, setCurrentLocation] = useState<LocationKey>('singapore')
const { flyTo, isMapReady } = useMap()
const flyToLocation = useCallback((location: LocationKey) => {
const config = LOCATIONS[location]
flyTo({
center: config.center,
zoom: config.zoom,
duration: 1000
})
setCurrentLocation(location)
}, [flyTo])
const flyToCustomLocation = useCallback((center: [number, number], zoom: number) => {
flyTo({
center,
zoom,
duration: 1000
})
}, [flyTo])
return {
currentLocation,
flyToLocation,
flyToCustomLocation,
locations: LOCATIONS,
isMapReady
}
}