Command palette

@yasinsensoy/native

React Native · APIs and utilities

Location

Location API.

11 exports

Import

import { BACKGROUND_LOCATION_TASK, Location, useLocation } from "@yasinsensoy/native";

Location

module

Location API. Permission: Permissions.request("location").

NameTypeDescription
isEnabled() => booleanWhether the device's location services are on.
getCurrentPosition(options?: CurrentPositionOptions) => Promise<LocationPosition>The current position.
getLastKnownPosition() => Promise<LocationPosition | null>The last position known to the device (fast; may be stale, null if none).
watchPosition(listener: (position: LocationPosition) => void, options?: WatchPositionOptions, onError?: (error: LocationError) => void) => () => voidWatches the position; returns a function that ends the subscription.
startBackgroundUpdates(options?: BackgroundLocationOptions) => Promise<void>Starts tracking location in the background: it continues while the app is in the background and after the user closes the UI too (an Android foreground service with a persistent notification; the iOS background location mode). Positions go to the Location.setBackgroundHandler handler. Its state is persisted: it continues even if the app restarts until Location.stopBackgroundUpdates is called. Requirements: ui.config location: { background: true }, the location permission (enough if started while the app is open). For restarting while the app is closed (iOS significantChanges, Android startOnBoot) the locationAlways permission too.
stopBackgroundUpdates() => Promise<void>Stops background location.
isBackgroundUpdatesActive() => booleanWhether background location is running (false if not in the build).
setBackgroundHandler(handler: BackgroundLocationHandler | null) => voidHandler of background positions. Call it outside components, in the app's root file (index.js), so it is defined even when the app is launched in the background. Positions arrive in batches; the next batch waits until the handler finishes. Positions arriving without a handler are kept on the native side.

Key of the headless JS task for background location on the native side (Android).

BACKGROUND_LOCATION_TASK: 

useLocation

function

Watches the position while the component is mounted. enabled: false (e.g. without permission) stops tracking.

useLocation(options?: WatchPositionOptions & { enabled?: boolean; }): UseLocationResult

Location. Unknown fields are null.

NameTypeDescription
latitudenumberLatitude (degrees).
longitudenumberLongitude (degrees).
altitudenumber | nullAltitude above sea level (m).
accuracynumber | nullHorizontal accuracy (m, radius).
altitudeAccuracynumber | nullVertical accuracy (m).
headingnumber | nullHeading (degrees, clockwise from north).
speednumber | nullSpeed (m/s).
timestampnumberMeasurement time (ms, epoch).
mockedbooleanWhether it comes from a mock location app (Android) / a simulation (iOS).

Accuracy: high (GPS, meter level, uses battery), balanced (~100 m), low (~1 km, network).

type LocationAccuracy = "high" | "balanced" | "low"

Options of Location.getCurrentPosition.

NameTypeDefaultDescription
accuracy?LocationAccuracy"balanced"
maximumAge?number0Returns immediately if a position this old (ms) is good enough.
timeout?number15000Maximum wait (ms).

Options of Location.watchPosition.

NameTypeDefaultDescription
accuracy?LocationAccuracy"balanced"
distanceInterval?number0Report after moving at least this far (m).
interval?number5000Android: update interval (ms). On iOS it is decided by the system.

LocationError

interface

Location error.

NameTypeDescription
codestringE_PERMISSION, E_DISABLED, E_TIMEOUT, E_LOCATION.
messagestringError message.

Options of Location.startBackgroundUpdates.

NameTypeDefaultDescription
accuracy?LocationAccuracy"balanced"
distanceInterval?number0Report after moving at least this far (m).
interval?number5000Android: update interval (ms).
batchInterval?number0Deliver positions in batches at most this often (ms) (for battery and network). 0 delivers each position at once.
maxBatchSize?number100Maximum positions delivered at once.
notification?{ title?: string; body?: string; /** Icon color (`#rrggbb`). */ color?: string; /** Name of the notification channel shown in settings. @defaultValue `"Konum"` */ channelName?: string; }Android: the persistent notification shown while the service runs (the system requires it). The default title is the app name.
startOnBoot?booleanfalseAndroid: start by itself when the device reboots. Needs the "always" location permission (locationAlways).
activityType?"other" | "automotive" | "fitness" | "navigation""other"iOS: activity type; the system behaves accordingly for battery.
pausesAutomatically?booleanfalseiOS: the system may pause updates while stationary.
showsIndicator?booleantrueiOS: a location indicator in the status bar while in the background.
significantChanges?booleanfalseiOS: also monitor significant location changes; even if the user closes the app, the system relaunches it in the background. Needs the "always" permission (locationAlways).

Background location handler; the next batch comes when the returned Promise settles.

type BackgroundLocationHandler = (locations: LocationPosition[]) => Promise<void> | void

Result of useLocation.

NameTypeDescription
positionLocationPosition | nullThe last position (null before the first measurement).
errorLocationError | nullThe last error.