Skip to main content

@rtorcato/browser-common / geolocation

geolocation

Functions​

isGeolocationAvailable()​

isGeolocationAvailable(): boolean

Defined in: geolocation/index.ts:10

Checks if the Geolocation API is available in the browser.

Example​

import { isGeolocationAvailable } from '@rtorcato/browser-common/geolocation'
if (isGeolocationAvailable()) askForLocation()

Returns​

boolean

True if geolocation is available, false otherwise.


getCurrentPosition()​

getCurrentPosition(options?): Promise<GeolocationPosition>

Defined in: geolocation/index.ts:27

Gets the current position using the browser's Geolocation API.

Example​

import { getCurrentPosition } from '@rtorcato/browser-common/geolocation'
const pos = await getCurrentPosition({ enableHighAccuracy: true })
console.log(pos.coords.latitude, pos.coords.longitude)

Parameters​

options?​

PositionOptions

Optional PositionOptions for geolocation.

Returns​

Promise<GeolocationPosition>

Resolves with the position or rejects with an error.

Remarks​

Requires HTTPS and an explicit permission grant.


watchPosition()​

watchPosition(success, error?, options?): number | undefined

Defined in: geolocation/index.ts:52

Watches the user's position and calls the callback on each update.

Example​

import { watchPosition, clearWatch } from '@rtorcato/browser-common/geolocation'
const id = watchPosition((pos) => console.log(pos.coords))
if (id !== undefined) clearWatch(id)

Parameters​

success​

PositionCallback

Callback for successful position update.

error?​

PositionErrorCallback

Optional callback for errors.

options?​

PositionOptions

Optional PositionOptions for geolocation.

Returns​

number | undefined

The watch ID, or undefined if not available.

Remarks​

Requires HTTPS and an explicit permission grant.


clearWatch()​

clearWatch(watchId): void

Defined in: geolocation/index.ts:70

Clears a geolocation watch by its ID.

Example​

import { clearWatch } from '@rtorcato/browser-common/geolocation'
clearWatch(watchId)

Parameters​

watchId​

number

The watch ID returned by watchPosition.

Returns​

void