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

GPS Data

Location Finder

Uses the Geolocation API to display your current coordinates.

Triangulating...

API Reference

function useGeolocation(options?: PositionOptions): GeolocationState

Parameters

NameTypeDescription
options?PositionOptionsConfiguration 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>
}