React Native · APIs and utilities
Location
Location API.
11 exports
Import
Location
moduleLocation API. Permission: Permissions.request("location").
| Name | Type | Description |
|---|---|---|
isEnabled | () => boolean | Whether 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) => () => void | Watches 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 | () => boolean | Whether background location is running (false if not in the build). |
setBackgroundHandler | (handler: BackgroundLocationHandler | null) => void | Handler 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. |
BACKGROUND_LOCATION_TASK
constantKey of the headless JS task for background location on the native side (Android).
useLocation
functionWatches the position while the component is mounted. enabled: false (e.g. without permission) stops tracking.
LocationPosition
interfaceLocation. Unknown fields are null.
| Name | Type | Description |
|---|---|---|
latitude | number | Latitude (degrees). |
longitude | number | Longitude (degrees). |
altitude | number | null | Altitude above sea level (m). |
accuracy | number | null | Horizontal accuracy (m, radius). |
altitudeAccuracy | number | null | Vertical accuracy (m). |
heading | number | null | Heading (degrees, clockwise from north). |
speed | number | null | Speed (m/s). |
timestamp | number | Measurement time (ms, epoch). |
mocked | boolean | Whether it comes from a mock location app (Android) / a simulation (iOS). |
LocationAccuracy
typeAccuracy: high (GPS, meter level, uses battery), balanced (~100 m), low (~1 km, network).
CurrentPositionOptions
interfaceOptions of Location.getCurrentPosition.
| Name | Type | Default | Description |
|---|---|---|---|
accuracy? | LocationAccuracy | "balanced" | |
maximumAge? | number | 0 | Returns immediately if a position this old (ms) is good enough. |
timeout? | number | 15000 | Maximum wait (ms). |
WatchPositionOptions
interfaceOptions of Location.watchPosition.
| Name | Type | Default | Description |
|---|---|---|---|
accuracy? | LocationAccuracy | "balanced" | |
distanceInterval? | number | 0 | Report after moving at least this far (m). |
interval? | number | 5000 | Android: update interval (ms). On iOS it is decided by the system. |
LocationError
interfaceLocation error.
| Name | Type | Description |
|---|---|---|
code | string | E_PERMISSION, E_DISABLED, E_TIMEOUT, E_LOCATION. |
message | string | Error message. |
BackgroundLocationOptions
interfaceOptions of Location.startBackgroundUpdates.
| Name | Type | Default | Description |
|---|---|---|---|
accuracy? | LocationAccuracy | "balanced" | |
distanceInterval? | number | 0 | Report after moving at least this far (m). |
interval? | number | 5000 | Android: update interval (ms). |
batchInterval? | number | 0 | Deliver positions in batches at most this often (ms) (for battery and network). 0 delivers each position at once. |
maxBatchSize? | number | 100 | Maximum 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? | boolean | false | Android: 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? | boolean | false | iOS: the system may pause updates while stationary. |
showsIndicator? | boolean | true | iOS: a location indicator in the status bar while in the background. |
significantChanges? | boolean | false | iOS: 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.
UseLocationResult
interfaceResult of useLocation.
| Name | Type | Description |
|---|---|---|
position | LocationPosition | null | The last position (null before the first measurement). |
error | LocationError | null | The last error. |