fn
useGeolocation
v0.0.14testeddemoReactive Geolocation API. Watches the device position, exposing
reactive coordinates, error, and readiness state, plus pause/resume controls
and a one-shot getCurrentPosition.
Examples
ts
const { coords, locatedAt, error, ready } = useGeolocation();ts
// One-shot fetch without a continuous watch
const { getCurrentPosition } = useGeolocation({ immediate: false });
const position = await getCurrentPosition();Demo
Loading demo…
Signature
ts
export function useGeolocation(options: UseGeolocationOptions ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
options? | UseGeolocationOptions | Options |
Returns
UseGeolocationReturnReactive position, error, readiness, and watch controls| Property | Type | Description |
|---|---|---|
isSupported | Readonly<Ref<boolean>> | Whether the Geolocation API is supported in the current environment. |
coords | Readonly<Ref<Omit<GeolocationPosition['coords'], 'toJSON'>>> | The most recent set of coordinates. |
locatedAt | Readonly<Ref<number | null>> | The timestamp of the most recent position, or null before the first fix. |
error | Readonly<Ref<GeolocationPositionError | null>> | The most recent error, or null if none. |
ready | Readonly<Ref<boolean>> | Whether at least one position fix has been received. |
isActive | Readonly<Ref<boolean>> | Whether the position is currently being watched. |
resume | () => void | Start watching the position. |
pause | () => void | Stop watching the position. |
getCurrentPosition | () => Promise<GeolocationPosition> | Request the current position once, without starting a continuous watch.
Resolves with the position (and updates coords/locatedAt) or rejects
with a GeolocationPositionError. |