This repo hosts the free iOS implementation of Background Geolocation for React Native. The Android version is on sale now!
Cross-platform background geolocation module for React Native with battery-saving "circular stationary-region monitoring" and "stop detection".
Follows the React Native Modules spec.
import BackgroundGeolocation from "react-native-background-geolocation";
- API Documentation
- Advanced Geofencing
- Location Data Schema
- Error Codes
- Debugging Sounds
- Geofence Features
import BackgroundGeolocation from "react-native-background-geolocation";
var Foo = React.createClass({
componentWillMount() {
// This handler fires whenever bgGeo receives a location update.
BackgroundGeolocation.on('location', this.onLocation);
// This handler fires when movement states changes (stationary->moving; moving->stationary)
BackgroundGeolocation.on('motionchange', this.onMotionChange);
// Now configure the plugin.
BackgroundGeolocation.configure({
// Geolocation Config
desiredAccuracy: 0,
stationaryRadius: 25,
distanceFilter: 10,
// Activity Recognition
stopTimeout: 1,
// Application config
debug: true, // <-- enable for debug sounds & notifications
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
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 / SQLite config
url: 'http://posttestserver.com/post.php?dir=cordova-background-geolocation',
autoSync: true, // <-- POST each location immediately to server
params: { // <-- Optional HTTP params
"auth_token": "maybe_your_server_authenticates_via_token_YES?"
}
}, function(state) {
console.log("- BackgroundGeolocation is configured and ready: ", state.enabled);
if (!state.enabled) {
BackgroundGeolocation.start(function() {
console.log("- Start success");
});
}
});
}
// You must remove listeners when your component unmounts
componentWillUnmount() {
// Remove BackgroundGeolocation listeners
BackgroundGeolocation.un('location', this.onLocation);
BackgroundGeolocation.un('motionchange', this.onMotionChange);
}
onLocation(location) {
console.log('- [js]location: ', JSON.stringify(location));
}
onMotionChange(location) {
console.log('- [js]motionchanged: ', JSON.stringify(location));
}
});
A fully-featured Demo App is available in its own public repo. After first cloning that repo, follow the installation instructions in the README there. This demo-app includes a settings-screen allowing you to quickly experiment with all the different settings available for each platform.
Yes it does. See the Wiki
- on iOS, background tracking won't be engaged until you travel about 2-3 city blocks, so go for a walk or car-ride (or use the Simulator with
Debug->Location->City Drive
) - Android is much quicker detecting movements; typically several meters of walking will do it.
- When in doubt, nuke everything: First delete the app from your device (or simulator)
The plugin has features allowing you to control the behaviour of background-tracking, striking a balance between accuracy and battery-usage. In stationary-mode, the plugin attempts to descrease its power usage and accuracy by setting up a circular stationary-region of configurable #stationaryRadius.
iOS has a nice system Significant Changes API, which allows the os to suspend your app until a cell-tower change is detected (typically 2-3 city-block change)
The plugin will execute your configured event-listener subscribed to by the onLocation(callback)
method. Both iOS & Android use a SQLite database to persist every recorded geolocation so you don't have to worry about persistence when no network is detected. The plugin provides a Javascript API to fetch and destroy the records in the database. In addition, the plugin has an optional HTTP layer allowing allowing you to automatically HTTP POST recorded geolocations to your server.
The function changePace({Boolean})
is provided to force the plugin to enter moving
or stationary
state.
The plugin uses iOS Significant Changes API, and starts triggering your configured callback
only when a cell-tower switch is detected (i.e. the device exits stationary radius).
When the plugin detects the device has moved beyond its configured #stationaryRadius
(typically 2-3 city blocks), it engages the native location API for aggressive monitoring according to the configured #desiredAccuracy
, #distanceFilter
.
The plugin will continue tracking the device's location even after the user closes the app (stopOnTerminate: false
) or reboots the device.
The MIT License (MIT)
Copyright (c) 2015 Chris Scott, Transistor Software
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.