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.

Returns

boolean

True if geolocation is available, false otherwise.

Example

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

getCurrentPosition()

getCurrentPosition(options?): Promise<GeolocationPosition>

Defined in: geolocation/index.ts:27

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

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.

Example

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

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.

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.

Example

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

clearWatch()

clearWatch(watchId): void

Defined in: geolocation/index.ts:70

Clears a geolocation watch by its ID.

Parameters

watchId

number

The watch ID returned by watchPosition.

Returns

void

Example

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