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
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
Full Signature
enableHighAccuracy?union
Full Signature
maximumAge?number
Full Signature
timeout?number
Full Signature
GeolocationState
Reactive geolocation coordinates and status returned by the hook.
Property
Specification
isSupportedunion
Whether
navigator.geolocation is supported in the current environmentFull Signature
loadingunion
Whether the initial position is currently being resolved
Full Signature
accuracyunion
Accuracy of latitude and longitude coordinates in meters
Full Signature
altitudeunion
Altitude in meters above the WGS84 ellipsoid, or
nullFull Signature
altitudeAccuracyunion
Accuracy of the altitude value in meters, or
nullFull Signature
headingunion
Direction of travel in degrees (
0–360), or nullFull Signature
latitudeunion
Latitude in decimal degrees, or
nullFull Signature
longitudeunion
Longitude in decimal degrees, or
nullFull Signature
speedunion
Velocity in meters per second, or
nullFull Signature
timestampunion
Timestamp when the position was acquired, or
nullFull Signature
errorunion
Latest
GeolocationPositionError, or nullFull Signature