Skip to main content

geolocation

Read the device's location, once or as a continuous watch, via the Geolocation API.

Import: @rtorcato/browser-common/geolocation

๐Ÿ“– MDN: Geolocation API ยท ๐Ÿ“Š caniuse: geolocation

Requires a secure context (HTTPS, or localhost in dev) and prompts the user for permission on first use.

Exampleโ€‹

import {
isGeolocationAvailable,
getCurrentPosition,
watchPosition,
clearWatch,
} from '@rtorcato/browser-common/geolocation'

if (isGeolocationAvailable()) {
const position = await getCurrentPosition({ enableHighAccuracy: true })
console.log(position.coords.latitude, position.coords.longitude)

const watchId = watchPosition((p) => updateMap(p.coords))
// later:
if (watchId !== undefined) clearWatch(watchId)
}

Exportsโ€‹

  • isGeolocationAvailable() โ€” feature check
  • getCurrentPosition(options?) โ€” resolve a single GeolocationPosition
  • watchPosition(success, error?, options?) โ€” subscribe to updates; returns a watch id
  • clearWatch(id) โ€” stop a watch

See the API reference for full signatures.