useGeolocation
Reactive Geolocation API. It allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information.
Demo
{
  "coords": {
    "accuracy": 0,
    "latitude": null,
    "longitude": null,
    "altitude": null,
    "altitudeAccuracy": null,
    "heading": null,
    "speed": null
  },
  "locatedAt": null,
  "error": null
}Usage
import { useGeolocation } from '@vueuse/core'
const { coords, locatedAt, error } = useGeolocation()
import { useGeolocation } from '@vueuse/core'
const { coords, locatedAt, error } = useGeolocation()
| State | Type | Description | 
|---|---|---|
| coords | Coordinates | information about the position retrieved like the latitude and longitude | 
| locatedAt | Date | The time of the last geolocation call | 
| error | string | An error message in case geolocation API fails. | 
Config
useGeolocationfunction takes PositionOptions object as an optional parameter.
Component
This function also provides a renderless component version via the@vueuse/components package. Learn more about the usage. <UseGeolocation v-slot="{ coords: { latitude, longitude } }">
  Latitude: {{ latitude }}
  Longitude: {{ longitude }}
</UseGeolocation>
<UseGeolocation v-slot="{ coords: { latitude, longitude } }">
  Latitude: {{ latitude }}
  Longitude: {{ longitude }}
</UseGeolocation>
Type Declarations
export interface GeolocationOptions
  extends Partial<PositionOptions>,
    ConfigurableNavigator {}
/**
 * Reactive Geolocation API.
 *
 * @see https://vueuse.org/useGeolocation
 * @param options
 */
export declare function useGeolocation(options?: GeolocationOptions): {
  isSupported: boolean | undefined
  coords: Ref<GeolocationCoordinates>
  locatedAt: Ref<number | null>
  error: Ref<{
    readonly code: number
    readonly message: string
    readonly PERMISSION_DENIED: number
    readonly POSITION_UNAVAILABLE: number
    readonly TIMEOUT: number
  } | null>
}
export declare type UseGeolocationReturn = ReturnType<typeof useGeolocation>
export interface GeolocationOptions
  extends Partial<PositionOptions>,
    ConfigurableNavigator {}
/**
 * Reactive Geolocation API.
 *
 * @see https://vueuse.org/useGeolocation
 * @param options
 */
export declare function useGeolocation(options?: GeolocationOptions): {
  isSupported: boolean | undefined
  coords: Ref<GeolocationCoordinates>
  locatedAt: Ref<number | null>
  error: Ref<{
    readonly code: number
    readonly message: string
    readonly PERMISSION_DENIED: number
    readonly POSITION_UNAVAILABLE: number
    readonly TIMEOUT: number
  } | null>
}
export declare type UseGeolocationReturn = ReturnType<typeof useGeolocation>
Source
Contributors
Changelog
v7.0.0 on 11/20/2021v6.0.0-beta.1 on 7/16/20217cb1f - feat(createSharedComposable): new functionv4.11.2 on 5/30/2021v5.0.0-beta.2 on 5/25/2021