59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { useState, useCallback } from 'react'
|
|
import { useMap } from '@/app/map-context'
|
|
|
|
const LOCATIONS = {
|
|
usa: {
|
|
center: [-95.7129, 37.0902] as [number, number],
|
|
zoom: 4
|
|
},
|
|
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>('usa')
|
|
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])
|
|
|
|
const location = LOCATIONS[currentLocation]
|
|
|
|
return {
|
|
currentLocation,
|
|
location,
|
|
flyToLocation,
|
|
flyToCustomLocation,
|
|
locations: LOCATIONS,
|
|
isMapReady
|
|
}
|
|
}
|