Skip to content

useGeolocation

Package
Share - Core
Category
Description
reactive [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API)

Reactive Geolocation API. 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.

Demo

{
  "coords": {
    "accuracy": 0,
    "latitude": null,
    "longitude": null,
    "altitude": null,
    "altitudeAccuracy": null,
    "heading": null,
    "speed": null
  },
  "locatedAt": null,
  "error": null
}

Usage

js
import { useGeolocation } from '@hungpvq/shared-core';

const { coords, locatedAt, error, resume, pause } = useGeolocation();
StateTypeDescription
coordsCoordinatesinformation about the position retrieved like the latitude and longitude
locatedAtDateThe time of the last geolocation call
errorstringAn error message in case geolocation API fails.
resumefunctionControl function to resume updating geolocation
pausefunctionControl function to pause updating geolocation

Config

useGeolocation function takes PositionOptions object as an optional parameter.