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 checkgetCurrentPosition(options?)โ resolve a singleGeolocationPositionwatchPosition(success, error?, options?)โ subscribe to updates; returns a watch idclearWatch(id)โ stop a watch
See the API reference for full signatures.