Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
activityRecognitionInterval: 5000,
stopDetectionDelay: 1, // <-- minutes to delay after motion stops before engaging stop-detection system
stopTimeout: 30, // 30 minutes
activityType: 'Other',
// Application config
debug: false, // <-- 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: true, // <-- [Android] Allow the background-service to run headless when user closes the app.
startOnBoot: false // <-- [Android] Auto start background-service in headless mode when device is powered-up.
});
// This handler fires whenever bgGeo receives a location update.
BackgroundGeolocation.on('location', _onLocationChanged);
BackgroundGeolocation.on('motionchange', _onMotionChange);
PushNotificationIOS.addEventListener('localNotification', _onNotification);
AppState.addEventListener('change', _handleAppStateChange);
}
stopDetectionDelay: 1, // <-- minutes to delay after motion stops before engaging stop-detection system
stopTimeout: 30, // 30 minutes
activityType: 'Other',
// Application config
debug: false, // <-- 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: true, // <-- [Android] Allow the background-service to run headless when user closes the app.
startOnBoot: false // <-- [Android] Auto start background-service in headless mode when device is powered-up.
});
// This handler fires whenever bgGeo receives a location update.
BackgroundGeolocation.on('location', _onLocationChanged);
BackgroundGeolocation.on('motionchange', _onMotionChange);
PushNotificationIOS.addEventListener('localNotification', _onNotification);
AppState.addEventListener('change', _handleAppStateChange);
}
export function mountBackgroundTask(navigator) {
_navigator = navigator;
BackgroundGeolocation.configure({
desiredAccuracy: 0,
stationaryRadius: 100,
distanceFilter: 500,
locationUpdateInterval: 5000,
minimumActivityRecognitionConfidence: 80, // 0-100%. Minimum activity-confidence for a state-change
fastestLocationUpdateInterval: 5000,
activityRecognitionInterval: 5000,
stopDetectionDelay: 1, // <-- minutes to delay after motion stops before engaging stop-detection system
stopTimeout: 30, // 30 minutes
activityType: 'Other',
// Application config
debug: false, // <-- 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)
geoLocationListener = () => {
BackgroundGeolocation.on('location', (location) => {
//console.log('[event] location: ', location);
if (!location.sample) {
const newCoordinate = {
latitude: location.coords.latitude,
longitude: location.coords.longitude
};
const { distanceTravelled } = this.state;
this.addMarker(location);
this.setState({
distanceTravelled: distanceTravelled + this.calcDistance(newCoordinate),
prevLatLng: newCoordinate,
odometer: (location.odometer / 1000).toFixed(1)
});
}
this.setCenter(location)
getCurrentLocation = (resetGeo) => {
BackgroundGeolocation.getCurrentPosition((location) => {
//console.log('- getCurrentPosition success: ', location);
this.addMarker(location);
this.setCenter(location)
if (resetGeo) {
this.resetGeoLocation();
} else {
this.checkRunState();
}
}, (error) => {
console.warn('- getCurrentPosition error: ', error);
}, { persist: true, samples: 1 });
}
handleRunStateChange = async (state) => {
if (state) {
let runState = {};
switch (state) {
case 'running':
runState = { ...this.state.runState, state: 'running', runAt: new Date() };
BackgroundGeolocation.start();
this._interval = setInterval(() => {
this.setState({ runningDuration: this.state.runningDuration + 1 })
}, 1000);
break;
case 'paused':
runState = { ...this.state.runState, state: 'paused', pauseAt: new Date() };
clearInterval(this._interval);
BackgroundGeolocation.stopWatchPosition();
break;
case 'resume':
runState = { ...this.state.runState, state: 'running' };
BackgroundGeolocation.watchPosition();
this._interval = setInterval(() => {
this.setState({ runningDuration: this.state.runningDuration + 1 })
}, 1000);
break;
function _handleAppStateChange(currentAppState) {
_appState = currentAppState;
if (currentAppState === 'background') {
BackgroundGeolocation.start();
} else if (currentAppState === 'active') {
BackgroundGeolocation.stop();
}
}
function _handleAppStateChange(currentAppState) {
_appState = currentAppState;
if (currentAppState === 'background') {
BackgroundGeolocation.start();
} else if (currentAppState === 'active') {
BackgroundGeolocation.stop();
}
}
case 'paused':
runState = { ...this.state.runState, state: 'paused', pauseAt: new Date() };
clearInterval(this._interval);
BackgroundGeolocation.stopWatchPosition();
break;
case 'resume':
runState = { ...this.state.runState, state: 'running' };
BackgroundGeolocation.watchPosition();
this._interval = setInterval(() => {
this.setState({ runningDuration: this.state.runningDuration + 1 })
}, 1000);
break;
case 'finished':
runState = { ...this.state.runState, state: 'finished', finishAt: new Date() };
clearInterval(this._interval);
BackgroundGeolocation.stop();
break;
}
await AsyncStorage.setItem(`${Configs.StorageKey}:runstate`, JSON.stringify(runState, null));
this.setState({ runState })
}
}
resetGeoLocation = async () => {
await BackgroundGeolocation.setOdometer(0);
await BackgroundGeolocation.destroyLocations();
this.setState({ isLoading: false })
}