ImportantGeolocation requires explicit user consent. The browser will automatically display a permission prompt when getCurrentPosition or watchPosition is invoked.
NoteUsing watchPosition continuously can significantly impact battery life on mobile devices. Ensure you call clearWatch when the tracking is no longer needed.
The useGeolocation provides a structured interface for retrieving and monitoring the physical location of a device. It abstracts the imperative nature of the browser's navigation.geolocation into a declarative React state, handling support detection and permission lifecycle tracking automatically.
This hook is designed for applications requiring spatial awareness—such as maps, delivery services, or localized content—while maintaining a strict unopinionated approach by allowing the developer to decide exactly when to trigger tracking or single requests.
| Name | Type | Description |
|---|---|---|
| isSupported | boolean | Indicates if navigator.geolocation is available in the environment. |
| permissionState | GeolocationPermissionState | Current status of the permission: granted, denied, prompt, or unsupported. |
| position | GeolocationPosition | null | The most recently acquired set of coordinates and accuracy data. |
| error | GeolocationPositionError | null | The error object if the last location request failed. |
| getCurrentPosition | (options?: PositionOptions) => void | Triggers a one-time requests for the current location. |
| watchPosition | (options?: PositionOptions) => number | null | Starts a continuous watch of the position; returns a watcher ID. |
| clearWatch | (id: number) => void | Stops a watch session using the provided ID. |