How to use react-native-background-geolocation - 10 common examples

To help you get started, weโ€™ve selected a few react-native-background-geolocation examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github City-of-Helsinki / open-city-app / src / helpers / backgroundTask.js View on Github external
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);
}
github City-of-Helsinki / open-city-app / src / helpers / backgroundTask.js View on Github external
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);
}
github City-of-Helsinki / open-city-app / src / helpers / backgroundTask.js View on Github external
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)
github devnowcafe / runx / src / screens / Home / index.js View on Github external
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)
github devnowcafe / runx / src / screens / Home / index.js View on Github external
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 });
    }
github devnowcafe / runx / src / screens / Home / index.js View on Github external
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;
github City-of-Helsinki / open-city-app / src / helpers / backgroundTask.js View on Github external
function _handleAppStateChange(currentAppState) {
  _appState = currentAppState;

  if (currentAppState === 'background') {
    BackgroundGeolocation.start();
  } else if (currentAppState === 'active') {
    BackgroundGeolocation.stop();
  }
}
github City-of-Helsinki / open-city-app / src / helpers / backgroundTask.js View on Github external
function _handleAppStateChange(currentAppState) {
  _appState = currentAppState;

  if (currentAppState === 'background') {
    BackgroundGeolocation.start();
  } else if (currentAppState === 'active') {
    BackgroundGeolocation.stop();
  }
}
github devnowcafe / runx / src / screens / Home / index.js View on Github external
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 })
        }
    }
github devnowcafe / runx / src / screens / Home / index.js View on Github external
resetGeoLocation = async () => {
        await BackgroundGeolocation.setOdometer(0);
        await BackgroundGeolocation.destroyLocations();
        this.setState({ isLoading: false })
    }