|
| 1 | +import { ref, onMounted, onUnmounted, Ref } from '@src/api' |
| 2 | + |
| 3 | +export interface UseGeolocation { |
| 4 | + loading: boolean |
| 5 | + accuracy: number | null |
| 6 | + altitude: number | null |
| 7 | + altitudeAccuracy: number | null |
| 8 | + heading: number | null |
| 9 | + latitude: number | null |
| 10 | + longitude: number | null |
| 11 | + speed: number | null |
| 12 | + timestamp: number | null |
| 13 | + error?: Error | PositionError |
| 14 | +} |
| 15 | + |
| 16 | +const defaultOpts = { |
| 17 | + enableHighAccuracy: false, |
| 18 | + timeout: Infinity, |
| 19 | + maximumAge: 0 |
| 20 | +} |
| 21 | + |
| 22 | +export function useGeolocation( |
| 23 | + options: PositionOptions = {}, |
| 24 | + runOnMount = true |
| 25 | +) { |
| 26 | + options = Object.assign(defaultOpts, options) |
| 27 | + |
| 28 | + // Note: surprisingly the watchId can be 0 (not positive) so |
| 29 | + // we have to check if watchId !== null every time |
| 30 | + let watchId: number | null = null |
| 31 | + |
| 32 | + const geoInitData = { |
| 33 | + loading: false, |
| 34 | + accuracy: null, |
| 35 | + altitude: null, |
| 36 | + altitudeAccuracy: null, |
| 37 | + heading: null, |
| 38 | + latitude: null, |
| 39 | + longitude: null, |
| 40 | + speed: null, |
| 41 | + timestamp: null |
| 42 | + } |
| 43 | + |
| 44 | + const isTracking = ref(false) |
| 45 | + const geo: Ref<UseGeolocation> = ref({ ...geoInitData }) |
| 46 | + |
| 47 | + const onEventError = (error: PositionError) => { |
| 48 | + if (watchId === null) return |
| 49 | + geo.value.loading = false |
| 50 | + geo.value.error = { |
| 51 | + code: error.code, |
| 52 | + message: error.message |
| 53 | + } as PositionError |
| 54 | + isTracking.value = false |
| 55 | + } |
| 56 | + |
| 57 | + const handleGeolocation = ({ coords, timestamp }: Position) => { |
| 58 | + geo.value = { |
| 59 | + loading: false, |
| 60 | + accuracy: coords.accuracy, |
| 61 | + altitude: coords.altitude, |
| 62 | + altitudeAccuracy: coords.altitudeAccuracy, |
| 63 | + heading: coords.heading, |
| 64 | + latitude: coords.latitude, |
| 65 | + longitude: coords.longitude, |
| 66 | + speed: coords.speed, |
| 67 | + timestamp |
| 68 | + } |
| 69 | + isTracking.value = true |
| 70 | + } |
| 71 | + |
| 72 | + const start = () => { |
| 73 | + if (watchId !== null) return |
| 74 | + geo.value.loading = true |
| 75 | + geo.value.timestamp = Date.now() |
| 76 | + |
| 77 | + navigator.geolocation.getCurrentPosition( |
| 78 | + handleGeolocation, |
| 79 | + onEventError, |
| 80 | + options |
| 81 | + ) |
| 82 | + |
| 83 | + watchId = navigator.geolocation.watchPosition( |
| 84 | + handleGeolocation, |
| 85 | + onEventError, |
| 86 | + options |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + const stop = () => { |
| 91 | + if (watchId === null) return |
| 92 | + navigator.geolocation.clearWatch(watchId) |
| 93 | + watchId = null |
| 94 | + isTracking.value = false |
| 95 | + } |
| 96 | + |
| 97 | + onMounted(() => runOnMount && start()) |
| 98 | + onUnmounted(stop) |
| 99 | + |
| 100 | + return { isTracking, geo, start, stop } |
| 101 | +} |
0 commit comments