Skip to Content
ohooks

useGeolocation

Watches the user's geographic location reactively using navigator.geolocation.watchPosition.

Loading preview...

The useGeolocation hook subscribes to device position updates via navigator.geolocation.watchPosition and returns live coordinates, accuracy metrics, loading status, and error state.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useGeolocation.json

Usage

Examples

Displaying Live Coordinates

Render a loading indicator until the browser resolves the first position fix, and handle permission errors gracefully.

Conditional Location Tracking (enabled)

Set enabled: false until the user explicitly opts in so the browser does not prompt for location permission on page load.

API Reference

UseGeolocationOptions

Configuration options extending standard PositionOptions with an enabled flag.

Property
Specification
enabled?
uniondefault:true
Whether geolocation watching is active
enableHighAccuracy?
union
maximumAge?
number
timeout?
number

GeolocationState

Reactive geolocation coordinates and status returned by the hook.

Property
Specification
isSupported
union
Whether navigator.geolocation is supported in the current environment
loading
union
Whether the initial position is currently being resolved
accuracy
union
Accuracy of latitude and longitude coordinates in meters
altitude
union
Altitude in meters above the WGS84 ellipsoid, or null
altitudeAccuracy
union
Accuracy of the altitude value in meters, or null
heading
union
Direction of travel in degrees (0–360), or null
latitude
union
Latitude in decimal degrees, or null
longitude
union
Longitude in decimal degrees, or null
speed
union
Velocity in meters per second, or null
timestamp
union
Timestamp when the position was acquired, or null
error
union
Latest GeolocationPositionError, or null