fn

useGeolocation

v0.0.14testeddemo

Reactive 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

ParameterTypeDescription
options?UseGeolocationOptionsOptions

Returns

UseGeolocationReturnReactive position, error, readiness, and watch controls
PropertyTypeDescription
isSupportedReadonly<Ref<boolean>>Whether the Geolocation API is supported in the current environment.
coordsReadonly<Ref<Omit<GeolocationPosition['coords'], 'toJSON'>>>The most recent set of coordinates.
locatedAtReadonly<Ref<number | null>>The timestamp of the most recent position, or null before the first fix.
errorReadonly<Ref<GeolocationPositionError | null>>The most recent error, or null if none.
readyReadonly<Ref<boolean>>Whether at least one position fix has been received.
isActiveReadonly<Ref<boolean>>Whether the position is currently being watched.
resume() => voidStart watching the position.
pause() => voidStop 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.