The most sophisticated cross-platform background location-tracking & geofencing module with battery-conscious motion-detection intelligence
npm install react-native-background-geolocation|
| Now with Expo support |
| --- | --- |
Background Geolocation for React Native · ![npm]() ![npm]()
============================================================================

-------------------------------------------------------------------------------
The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
The plugin's Philosophy of Operation is to use motion-detection APIs (using accelerometer, gyroscope and magnetometer) to detect when the device is moving and stationary.
- When the device is detected to be moving, the plugin will automatically start recording a location according to the configured distanceFilter (meters).
- When the device is detected be stationary, the plugin will automatically turn off location-services to conserve energy.
Also available for Flutter, Cordova, Capacitor and pure native apps.
> [!NOTE]
> The Android module requires purchasing a license. However, it will work for DEBUG builds. It will not work with RELEASE builds without purchasing a license. This plugin is supported full-time and field-tested daily since 2013.
----------------------------------------------------------------------------

!Home
!Settings
> [!CAUTION]
> This README references the new __v5.x__ branch of the SDK. If you're using __v4.x__, you must reference __v4.x__ branch.
v4.x. __v4.x__ license keys do not work with __v5__. Login to the customer dashboard to generate a __v5__ key.
shell
npx expo install react-native-background-geolocation
`
$3
`bash
yarn add react-native-background-geolocation
`
$3
`
$ npm install react-native-background-geolocation --save
`
:large_blue_diamond: Setup Guides
$3
- Expo Setup
$3
- Auto-linking Setup
$3
- Auto-linking Setup
:large_blue_diamond: Configure your license
1. Login to Customer Dashboard to generate an application key:
www.transistorsoft.com/shop/customers

2. __[Android]__ Add your license-key to android/app/src/main/AndroidManifest.xml:
`diff
package="com.transistorsoft.backgroundgeolocation.react">
android:name=".MainApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
+
.
.
.
`
3. __[iOS]__ Add your license-key to your __Info.plist__
| Key | Type | Value |
|-----|-------|-------------|
| TSLocationManagerLicense | String | |
__TSLocationManagerLicense__. Paste the contents of your license key into the __value__.
:large_blue_diamond: Using the plugin ##
`javascript
import BackgroundGeolocation from "react-native-background-geolocation";
`
:large_blue_diamond: Example
There are three main steps to using BackgroundGeolocation
1. Wire up event-listeners.
2. .ready(config) the plugin.
3. .start() the plugin.
> [!WARNING]
> Do not execute any API method which will require accessing location-services until the .ready(config) method resolves (Read its API docs), For example:
>- .getCurrentPosition
>- .watchPosition
>- .start
`javascript
// NO! .ready() has not resolved.
await BackgroundGeolocation.getCurrentPosition(options);
await BackgroundGeolocation.start();
// First call .ready(config)
const state = await BackgroundGeolocation.ready(config);
const location = await BackgroundGeolocation.getCurrentPosition(options);
await BackgroundGeolocation.start();
// NO! .ready() has not resolved.
const location = await BackgroundGeolocation.getCurrentPosition(options);
await BackgroundGeolocation.start();
`
$3
`javascript
import React from 'react';
import {
Switch,
Text,
View,
} from 'react-native';
import BackgroundGeolocation, {
Location,
Subscription
} from "react-native-background-geolocation";
const HelloWorld = () => {
const [enabled, setEnabled] = React.useState(false);
const [location, setLocation] = React.useState('');
React.useEffect(() => {
/// 1. Subscribe to events.
const onLocation:Subscription = BackgroundGeolocation.onLocation((location) => {
console.log('[onLocation]', location);
setLocation(JSON.stringify(location, null, 2));
})
const onMotionChange:Subscription = BackgroundGeolocation.onMotionChange((event) => {
console.log('[onMotionChange]', event);
});
const onActivityChange:Subscription = BackgroundGeolocation.onActivityChange((event) => {
console.log('[onActivityChange]', event);
})
const onProviderChange:Subscription = BackgroundGeolocation.onProviderChange((event) => {
console.log('[onProviderChange]', event);
})
/// 2. ready the plugin.
BackgroundGeolocation.ready({
// Geolocation Config
geolocation: {
desiredAccuracy: BackgroundGeolocation.DesiredAccuracy.High
distanceFilter: 10,
stopTimeout: 5
},
// Logger Config
logger: {
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
logLevel: BackgroundGeolocation.LogLevel.Verbose
},
// Application config
app: {
stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
startOnBoot: true // <-- Auto start tracking when device is powered-up.
},
// Http Config
http: {
url: 'http://yourserver.com/locations',
batchSync: false, // <-- [Default: false] Set true to sync locations to server in a single HTTP request.
autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
headers: { // <-- Optional HTTP headers
"X-FOO": "bar"
},
params: { // <-- Optional HTTP params
"auth_token": "maybe_your_server_authenticates_via_token_YES?"
}
}
}).then((state) => {
setEnabled(state.enabled)
console.log("- BackgroundGeolocation is configured and ready: ", state.enabled);
});
return () => {
// Remove BackgroundGeolocation event-subscribers when the View is removed or refreshed
// during development live-reload. Without this, event-listeners will accumulate with
// each refresh during live-reload.
onLocation.remove();
onMotionChange.remove();
onActivityChange.remove();
onProviderChange.remove();
}
}, []);
/// 3. start / stop BackgroundGeolocation
React.useEffect(() => {
if (enabled) {
BackgroundGeolocation.start();
} else {
BackgroundGeolocation.stop();
setLocation('');
}
}, [enabled]);
return (
Click to enable BackgroundGeolocation
{location}
)
}
export default HelloWorld;
``