useGeolocation

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.

Example

Basic Usage

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

Default usage

Get current geolocation information

API

const {  coords,  locatedAt,  isLoading,  error,  resume,  pause} = useGeolocation(options:? Options)
const {  coords,  locatedAt,  isLoading,  error,  resume,  pause} = useGeolocation(options:? Options)

Params

PropertyDescriptionTypeDefault
options设置Options-

Options

PropertyDescriptionTypeDefault
onError错误时触发(error: GeolocationPositionError) => void-
immediate首次渲染时立即触发booleanfalse

Result

PropertyDescriptionType
coordsLocation informationAccessor<GeolocationCoordinates>
locatedAtThe time of the last geolocation callAccessor<number | null>
isLoadingIs loadingAccessor<boolean>
erroran error message in case geolocation API failsAccessor<GeolocationPositionError | null>
resumeControl function to resume updating geolocation() => Promise<{ locatedAt: number; coords: GeolocationCoordinates }>
pauseControl function to pause updating geolocation() => void