Cross-platform background geolocation module for React Native
Chris Scott has "just started" porting his Cordova Background Geolocation to React Native. This shift of modules coming from Cordova is something we'd love to see in the upcoming future.
His work, coming from the expertise of his previous module, is just invaluable (and also greatly documented). Check it out!
react-native-background-geolocation on GitHub (demo)
Cross-platform background geolocation module for React Native with battery-saving "circular stationary-region monitoring" and "stop detection".
This is a React Native implementation of our popular Cordova Background Geolocation plugin, a sophisticated, battery-conscious location-management component for both iOS & Android.
var BackgroundGeolocation = require('react-native-background-geolocation');
var Foo = React.createClass({
getInitialState() {
BackgroundGeolocation.configure({
desiredAccuracy: 0,
stationaryRadius: 50,
distanceFilter: 50,
disableElasticity: false, // <-- [iOS] Default is 'false'. Set true to disable speed-based distanceFilter elasticity
locationUpdateInterval: 5000,
minimumActivityRecognitionConfidence: 80, // 0-100%. Minimum activity-confidence for a state-change
fastestLocationUpdateInterval: 5000,
activityRecognitionInterval: 10000,
stopTimeout: 0,
activityType: 'AutomotiveNavigation',
// Application config
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
forceReloadOnLocationChange: false, // <-- [Android] If the user closes the app **while location-tracking is started** , reboot app when a new location is recorded (WARNING: possibly distruptive to user)
forceReloadOnMotionChange: false, // <-- [Android] If the user closes the app **while location-tracking is started** , reboot app when device changes stationary-state (stationary->moving or vice-versa) --WARNING: possibly distruptive to user)
forceReloadOnGeofence: false, // <-- [Android] If the user closes the app **while location-tracking is started** , reboot app when a geofence crossing occurs --WARNING: possibly distruptive to user)
stopOnTerminate: false, // <-- [Android] Allow the background-service to run headless when user closes the app.
startOnBoot: true, // <-- [Android] Auto start background-service in headless mode when device is powered-up.
// HTTP / SQLite config
url: 'http://posttestserver.com/post.php?dir=cordova-background-geolocation',
batchSync: true, // <-- [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.
maxDaysToPersist: 1, // <-- Maximum days to persist a location in plugin's SQLite database when HTTP fails
headers: {
"X-FOO": "bar"
},
params: {
"auth_token": "maybe_your_server_authenticates_via_token_YES?"
}
});
// This handler fires whenever bgGeo receives a location update.
BackgroundGeolocation.on('location', function(location) {
console.log('- [js]location: ', JSON.stringify(location);
});
// This handler fires when movement states changes (stationary->moving; moving->stationary)
BackgroundGeolocation.on('motionchange', function(location) {
console.log('- [js]motionchanged: ', JSON.stringify(location));
});
BackgroundGeolocation.start(function() {
console.log('- [js] BackgroundGeolocation started successfully');
// Fetch current position
BackgroundGeolocation.getCurrentPosition(function(location) {
console.log('- [js] BackgroundGeolocation received current position: ', JSON.stringify(location));
});
});
// Call #stop to halt all tracking
// BackgroundGeolocation.stop();
}
});