Skip to content

⬅️ Back to Table of Contents

📄 useGeolocation

📊 Analysis Summary

Metric Count
🔧 Functions 4
📦 Imports 7
📐 Interfaces 2

📚 Table of Contents

🛠️ File Location:

📂 packages/core/useGeolocation/index.ts

📦 Imports

Name Source
ShallowRef vue
ConfigurableNavigator ../_configurable
Supportable ../types
tryOnScopeDispose @vueuse/shared
shallowRef vue
defaultNavigator ../_configurable
useSupported ../useSupported

Functions

useGeolocation(options: UseGeolocationOptions): UseGeolocationReturn

Reactive Geolocation API.

Parameters:

  • options any: No description

See: https://vueuse.org/useGeolocation

Raw JSDoc
/**
 * Reactive Geolocation API.
 *
 * @see https://vueuse.org/useGeolocation
 * @param options
 */

Calls:

  • useSupported (from ../useSupported)
  • shallowRef (from vue)
  • navigator!.geolocation.watchPosition
  • resume
  • navigator.geolocation.clearWatch
  • tryOnScopeDispose (from @vueuse/shared)
  • pause
Code
export function useGeolocation(options: UseGeolocationOptions = {}): UseGeolocationReturn {
  const {
    enableHighAccuracy = true,
    maximumAge = 30000,
    timeout = 27000,
    navigator = defaultNavigator,
    immediate = true,
  } = options

  const isSupported = useSupported(() => navigator && 'geolocation' in navigator)

  const locatedAt = shallowRef<number | null>(null)
  const error = shallowRef<GeolocationPositionError | null>(null)
  const coords = shallowRef<Omit<GeolocationPosition['coords'], 'toJSON'>>({
    accuracy: 0,
    latitude: Number.POSITIVE_INFINITY,
    longitude: Number.POSITIVE_INFINITY,
    altitude: null,
    altitudeAccuracy: null,
    heading: null,
    speed: null,
  })

  function updatePosition(position: GeolocationPosition) {
    locatedAt.value = position.timestamp
    coords.value = position.coords
    error.value = null
  }

  let watcher: number

  function resume() {
    if (isSupported.value) {
      watcher = navigator!.geolocation.watchPosition(
        updatePosition,
        err => error.value = err,
        {
          enableHighAccuracy,
          maximumAge,
          timeout,
        },
      )
    }
  }

  if (immediate)
    resume()

  function pause() {
    if (watcher && navigator)
      navigator.geolocation.clearWatch(watcher)
  }

  tryOnScopeDispose(() => {
    pause()
  })

  return {
    isSupported,
    coords,
    locatedAt,
    error,
    resume,
    pause,
  }
}

Internal helpers

Declared inside another function in this file.

updatePosition(position: GeolocationPosition): void

Parameters:

  • position GeolocationPosition

Returns: void

Code
function updatePosition(position: GeolocationPosition) {
    locatedAt.value = position.timestamp
    coords.value = position.coords
    error.value = null
  }

resume(): void

Returns: void

Calls:

  • navigator!.geolocation.watchPosition
Code
function resume() {
    if (isSupported.value) {
      watcher = navigator!.geolocation.watchPosition(
        updatePosition,
        err => error.value = err,
        {
          enableHighAccuracy,
          maximumAge,
          timeout,
        },
      )
    }
  }

pause(): void

Returns: void

Calls:

  • navigator.geolocation.clearWatch
Code
function pause() {
    if (watcher && navigator)
      navigator.geolocation.clearWatch(watcher)
  }

Interfaces

UseGeolocationOptions

Interface Code
export interface UseGeolocationOptions extends Partial<PositionOptions>, ConfigurableNavigator {
  immediate?: boolean
}

Properties

Name Type Optional Description
immediate boolean not shown

UseGeolocationReturn

Interface Code
export interface UseGeolocationReturn extends Supportable {
  coords: ShallowRef<Omit<GeolocationPosition['coords'], 'toJSON'>>
  locatedAt: ShallowRef<number | null>
  error: ShallowRef<GeolocationPositionError | null>
  resume: () => void
  pause: () => void
}

Properties

Name Type Optional Description
coords ShallowRef<Omit<GeolocationPosition['coords'], 'toJSON'>> not shown
locatedAt ShallowRef<number \| null> not shown
error ShallowRef<GeolocationPositionError \| null> not shown
resume () => void not shown
pause () => void not shown

Generated by Syntax Scribe