BackgroundGeolocation

Improve this doc

$ ionic plugin add cordova-plugin-mauron85-background-geolocation

Repo: https://github.com/mauron85/cordova-plugin-background-geolocation

This plugin provides foreground and background geolocation with battery-saving "circular region monitoring" and "stop detection". For more detail, please see https://github.com/mauron85/cordova-plugin-background-geolocation

Supported platforms

Usage

import { BackgroundGeolocation } from 'ionic-native';


// When device is ready :
platform.ready().then(() => {
    // IMPORTANT: BackgroundGeolocation must be called within app.ts and or before Geolocation. Otherwise the platform will not ask you for background tracking permission.

    // BackgroundGeolocation is highly configurable. See platform specific configuration options
    let config = {
            desiredAccuracy: 10,
            stationaryRadius: 20,
            distanceFilter: 30,
            debug: true, //  enable this hear sounds for background-geolocation life-cycle.
            stopOnTerminate: false, // enable this to clear background location settings when the app terminates
    };

    BackgroundGeolocation.configure((location) => {
         console.log('[js] BackgroundGeolocation callback:  ' + location.latitude + ',' + location.longitude);

          // IMPORTANT:  You must execute the finish method here to inform the native plugin that you're finished,
          // and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
          // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
          BackgroundGeolocation.finish(); // FOR IOS ONLY

     }, (error) => {
       console.log('BackgroundGeolocation error');
     }, config);

    // Turn ON the background-geolocation system.  The user will be tracked whenever they suspend the app.
    BackgroundGeolocation.start();
})

// If you wish to turn OFF background-tracking, call the #stop method.
BackgroundGeolocation.stop();

Static Members

LocationProvider

Set location service provider @see https://github.com/mauron85/cordova-plugin-background-geolocation/wiki/Android-providers


Possible values: ANDROID_DISTANCE_FILTER_PROVIDER: 0,
 ANDROID_ACTIVITY_PROVIDER: 1


Accuracy

Desired accuracy in meters. Possible values [0, 10, 100, 1000].
 The lower the number, the more power devoted to GeoLocation resulting in higher accuracy readings.
 1000 results in lowest power drain and least accurate readings.


Possible values: HIGH: 0
 MEDIUM: 10
 LOW: 100
 PASSIVE: 1000

enum {number}

Mode

Used in the switchMode function


Possible values: BACKGROUND: 0 FOREGROUND: 1


configure(callback, errorCallback, options)

Configure the plugin.

Param Type Details
callback Function

callback will be called when background location is determined.

errorCallback Function

callback to be executed every time a geolocation error occurs.

options Config

An object of type Config

Returns: Location object, which tries to mimic w3c Coordinates interface. See http://dev.w3.org/geo/api/spec-source.html#coordinates_interface Callback to be executed every time a geolocation is recorded in the background.

start()

Turn ON the background-geolocation system. The user will be tracked whenever they suspend the app.

Returns: Promise<any>

stop()

Turn OFF background-tracking

Returns: Promise<any>

finish()

Inform the native plugin that you’re finished, the background-task may be completed NOTE: IOS, WP only

changePace()

Force the plugin to enter “moving” or “stationary” state NOTE: IOS, WP only

setConfig()

Setup configuration

Returns: Promise<any>

getStationaryLocation()

Returns current stationaryLocation if available. null if not NOTE: IOS, WP only

Returns: Promise<Location>

onStationary()

Add a stationary-region listener. Whenever the devices enters “stationary-mode”, your #success callback will be executed with #location param containing #radius of region NOTE: IOS, WP only

Returns: Promise<any>

isLocationEnabled()

Check if location is enabled on the device

Returns: Promise<number> Returns a promise with int argument that takes values 0, 1 (true). NOTE: ANDROID only

showAppSettings()

Display app settings to change permissions

showLocationSettings()

Display device location settings

watchLocationMode()

Method can be used to detect user changes in location services settings. If user enable or disable location services then success callback will be executed. In case or error (SettingNotFoundException) fail callback will be executed. NOTE: ANDROID only

Returns: Promise<boolean>

stopWatchingLocationMode()

Stop watching for location mode changes. NOTE: ANDROID only

getLocations()

Method will return all stored locations. Locations are stored when: - config.stopOnTerminate is false and main activity was killed by the system or - option.debug is true NOTE: ANDROID only

Returns: Promise<any>

getValidLocations()

Method will return locations, which has not been yet posted to server. NOTE: Locations does contain locationId.


Returns: Promise<any>

deleteLocation()

Delete stored location by given locationId. NOTE: ANDROID only

Returns: Promise<any>

deleteAllLocations()

Delete all stored locations. NOTE: ANDROID only

Returns: Promise<any>

switchMode()

Normally plugin will handle switching between BACKGROUND and FOREGROUND mode itself. Calling switchMode you can override plugin behavior and force plugin to switch into other mode.

In FOREGROUND mode plugin uses iOS local manager to receive locations and behavior is affected by option.desiredAccuracy and option.distanceFilter. In BACKGROUND mode plugin uses significant changes and region monitoring to receive locations and uses option.stationaryRadius only.


BackgroundGeolocation.Mode.FOREGROUND BackgroundGeolocation.Mode.BACKGROUND


NOTE: iOS only

@param {number} See above.


Returns: Promise<any>

getLogEntries()

Return all logged events. Useful for plugin debugging. Parameter limit limits number of returned entries.
 @see https://github.com/mauron85/cordova-plugin-background-geolocation/tree/v2.2.1#debugging for more information.


@param {number} Limits the number of entries


Returns: Promise<any>

BackgroundGeolocationResponse

Param Type Details
locationId number

ID of location as stored in DB (or null)

serviceProvider string

Service provider

debug boolean

true if location recorded as part of debug

time number

UTC time of this fix, in milliseconds since January 1, 1970.

latitude number

latitude, in degrees.

longitude number

longitude, in degrees.

accuracy number

estimated accuracy of this location, in meters.

speed number

speed if it is available, in meters/second over ground.

altitude number

altitude if available, in meters above the WGS 84 reference ellipsoid.

altitudeAccuracy number

accuracy of the altitude if available.

bearing number

bearing, in degrees.

coords Coordinates

A Coordinates object defining the current location

timestamp number

A timestamp representing the time at which the location was retrieved.

BackgroundGeolocationConfig

Param Type Details
desiredAccuracy number

Desired accuracy in meters. Possible values [0, 10, 100, 1000]. The lower the number, the more power devoted to GeoLocation resulting in higher accuracy readings. 1000 results in lowest power drain and least accurate readings. @see Apple docs (https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instp/CLLocationManager/desiredAccuracy)

stationaryRadius number

Stationary radius in meters. When stopped, the minimum distance the device must move beyond the stationary location for aggressive background-tracking to engage.

debug
(optional)
boolean

When enabled, the plugin will emit sounds for life-cycle events of background-geolocation! See debugging sounds table.

distanceFilter number

The minimum distance (measured in meters) a device must move horizontally before an update event is generated. @see Apple docs. (https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instp/CLLocationManager/distanceFilter)

stopOnTerminate
(optional)
boolean

IOS, ANDROID ONLY Enable this in order to force a stop() when the application terminated (e.g. on iOS, double-tap home button, swipe away the app).o

Defaults to true

startOnBoot
(optional)
boolean

ANDROID ONLY
 Start background service on device boot.


Defaults to false

startForeground
(optional)
boolean

ANDROID ONLY
 If false location service will not be started in foreground and no notification will be shown.

Defaults to true

interval
(optional)
number

ANDROID, WP8 ONLY The minimum time interval between location updates in seconds.

notificationTitle
(optional)
string

ANDROID ONLY Custom notification title in the drawer.

notificationText
(optional)
string

ANDROID ONLY Custom notification text in the drawer.

notificationIconColor
(optional)
string

ANDROID ONLY The accent color to use for notification. Eg. #4CAF50.

notificationIconLarge
(optional)
string

ANDROID ONLY
 The filename of a custom notification icon. See android quirks.
 NOTE: Only available for API Level >=21.

notificationIconSmall
(optional)
string

ANDROID ONLY
 The filename of a custom notification icon. See android quirks.
 NOTE: Only available for API Level >=21.

locationProvider
(optional)
number

ANDROID ONLY Set location service provider @see wiki (https://github.com/mauron85/cordova-plugin-background-geolocation/wiki/Android-providers)

activityType
(optional)
string

IOS ONLY [AutomotiveNavigation, OtherNavigation, Fitness, Other] Presumably, this affects iOS GPS algorithm. @see Apple docs for more information (https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instp/CLLocationManager/activityType)

pauseLocationUpdates
(optional)
boolean

IOS ONLY
 Pauses location updates when app is paused


Defaults to true

url
(optional)
string

Server url where to send HTTP POST with recorded locations


syncUrl
(optional)
string

Server url where to send fail to post locations


syncThreshold
(optional)
number

Specifies how many previously failed locations will be sent to server at once


Defaults to 100

httpHeaders
(optional)
any

Optional HTTP headers sent along in HTTP request

saveBatteryOnBackground
(optional)
boolean

IOS ONLY
 Switch to less accurate significant changes and region monitory when in background (default)

Defaults to 100

maxLocations
(optional)
number

Limit maximum number of locations stored into db


Defaults to 10000

API

Native

General