React Native Android Background Geolocation
A react-native geolocation module for Android, which uses Fused Location Provider API to get location, and use the Service so that can run in the foreground and background even the app is terminated.
Example
See react-native-android-background-geolocation-demo
Installation
1. Install with npm
npm i --save react-native-android-background-geolocation
2. Link the native module
Automatic (Recommend)
Use react-native link
to automatic install android module.
react-native link react-native-android-background-geolocation
Manually
- in
android/app/build.gradle
:
dependencies {
+ compile project(':react-native-android-background-geolocation')
...
}
- in
android/settings.gradle
:
...
include ':app'
+ include ':react-native-android-background-geolocation'
+ project(':react-native-android-background-geolocation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-background-geolocation/android')
- in
MainApplication.java
:
+ ... @Override protected List<ReactPackage> { return Arrays.<ReactPackage>; }
3. Add the service
Add <service/>
to your AndroidManifest.xml
.
... ...
Usage
Run in foreground and background
You need to call LocationService.start()
and then register a listener by LocationService.onLocationChanged
.
<{}> async { // 1. Request permissions await this; // 2. Register a event listener by OnError and onLocationChanged LocationService; LocationService; } { // 3. Remove All listeners LocationService; } { LocationServicestart priority: PriorityModeEnumPRIORITY_HIGH_ACCURACY stopOnTerminate: false interval: 25 * 1000 fastestInterval: 20 * 1000 distanceFilter: 0 ; } { LocationService; } { return <View style=stylescontainer> <Text style=styleswelcome>Welcome to React Native!</Text> <TouchableOpacity style=stylesbutton onPress= this > <Text style=stylesbuttonText >Start Location Service</Text> </TouchableOpacity> <TouchableOpacity style=stylesbutton onPress= this > <Text style=stylesbuttonText >Stop Location Service</Text> </TouchableOpacity> </View> ; }
Continuously run when the app is terminated
The react-native-android-background-geolocation
will invoke your App in the background when the app is terminated. But that will not execute any react components, which means that only execute your entry file, usually is index.js
.
index.js
// @flow;;;; LocationService; AppRegistry;
Methods
LocationServicestartoption?);
Start tracking device location with specific options.
Parameters
NAME | TYPE | REQUIRED | DESCRIPTION |
---|---|---|---|
option | Option | No | Options for the Fused Location Provider API. |
LocationService;
Stop tracking device location.
let unsubscribe = LocationService;
Register a location change listener, and return the unsubscribe function.
see the LocationInfo type
let unsubscribe = LocationService;
Register an error listener, and return the unsubscribe function.
see the ErrorInfo type
Types
;...LocationServicestart priority: PriorityModeEnumPRIORITY_HIGH_ACCURACY stopOnTerminate: false interval: 25 * 1000 fastestInterval: 20 * 1000 distanceFilter: 0;...
Options for the location service.
Parameters
NAME | TYPE | REQUIRED | DESCRIPTION | DEFAULT |
---|---|---|---|---|
priority | PriorityModeEnum | YES | Set the priority of the request. | PRIORITY_BALANCED_POWER_ACCURACY |
stopOnTerminate | boolean | NO | Whether this service is stopped or not when the app is closed. | true |
interval | number | YES | The interval that can receive location information, in milliseconds. | 25000 ms |
fastestInterval | number | YES | The fastest interval that can receive location information, in milliseconds. | 20000 ms |
distanceFilter | number | YES | Set the minimum displacement between location updates in meters. | 100 m |
Structure
coords: latitude: number longitude: number
The location information is the argument passed to the event listener.
Structure
code: number message: string
The error code and an error message are the arguments passed to the event listener.
See more details of description at errors section.
Enums
;
This enum offers the priority of the start option.
VALUE | DESCRIPTION |
---|---|
PRIORITY_BALANCED_POWER_ACCURACY | This will mixed-use GPS or wifi and cell towers to get the location. |
PRIORITY_HIGH_ACCURACY | This will use GPS to get the finest location available but use more power. |
PRIORITY_LOW_POWER | This will use wifi or cell towers to get the location. |
PRIORITY_NO_POWER | Your app does not trigger any location updates, but receives locations triggered by other apps. |
;
This enum offers error codes that can be used at if
or switch-case
to distinguish the error.
VALUE |
---|
PERMISSIONS_DENIED |
PRIORITY_NOT_SUPPORTED |
GOOGLE_PLAY_SERVICE_UPDATING |
GOOGLE_PLAY_SERVICE_NEED_UPDATE |
GOOGLE_PLAY_SERVICE_ERROR |
Errors
CODE | ENUM VALUE | DESCRIPTION |
---|---|---|
40001 | PERMISSIONS_DENIED | No permissions to access a location service. |
40002 | PRIORITY_NOT_SUPPORTED | Use the unsupported priority. |
40003 | GOOGLE_PLAY_SERVICE_UPDATING | Google Play Services is updating. |
40004 | GOOGLE_PLAY_SERVICE_NEED_UPDATE | Google Play Services version is too old, needs to update. |
40005 | GOOGLE_PLAY_SERVICE_ERROR | Google Play Services is unavailable, please check is installed or not. |
License
MIT