Skip to main content

useGeolocation

React Sensor Hooks that tracks Geolocation

It allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information

Usage

Live Editor

function Demo() {
  const { coordinates, locatedAt, error } = useGeolocation();

  return (
    <div>
      <pre lang="json">
        {JSON.stringify(
          {
            coordinates: {
              accuracy: coordinates.accuracy,
              latitude: coordinates.latitude,
              longitude: coordinates.longitude,
              altitude: coordinates.altitude,
              altitudeAccuracy: coordinates.altitudeAccuracy,
              heading: coordinates.heading,
              speed: coordinates.speed,
            },
            locatedAt,
            error: error ? error.message : error,
          },
          null,
          2,
        )}
      </pre>
    </div>
  );
};

Result
Loading...

API

useGeoLocation

Returns

{ readonly coordinates: GeolocationCoordinates; readonly locatedAt: number | null; readonly error: GeolocationPositionError | null; readonly isSupported: boolean; }: A object with the following elements:

  • coordinates.
  • timestamp when get coordinates.
  • errors.
  • Whether the browser supports geolocation.

Arguments

ArgumentDescriptionTypeDefaultValue
optionsoptional PositionOptions paramsPartial<PositionOptions> | undefined-