useGeolocation
A hook to access the user's geolocation.
Overview
useGeolocation provides access to the browser's Geolocation API. It returns coordinates, accuracy, altitude, speed, and handles permissions/errors.
Demo
API Reference
function useGeolocation(options?: PositionOptions): GeolocationStateParameters
| Name | Type | Description |
|---|---|---|
options? | PositionOptions | Configuration object (enableHighAccuracy, timeout, maximumAge). |
Usage Example
import { useGeolocation } from 'react-hooks-ts'
function LocationDisplay() {
const { latitude, longitude, error, loading } = useGeolocation()
if (loading) return <p>Locating...</p>
if (error) return <p>Error: {error.message}</p>
return <p>Lat: {latitude}, Lng: {longitude}</p>
}