Background and foreground geolocation plugin for Cordova.





GitHub Stars



Last Commit

1yr ago








Type Definitions





🖕🖕🖕 Pkg has been moved to @mauron85/cordova-plugin-background-geolocation. Please update your dependencies.



We're moving

Npm package is now @mauron85/cordova-plugin-background-geolocation!

Submitting issues

All new issues should follow instructions in ISSUE_TEMPLATE.md. A properly filled issue report will significantly reduce number of follow up questions and decrease issue resolving time. Most issues cannot be resolved without debug logs. Please try to isolate debug lines related to your issue. Instructions for how to prepare debug logs can be found in section Debugging. If you're reporting an app crash, debug logs might not contain all the necessary information about the cause of the crash. In that case, also provide relevant parts of output of adb logcat command.

Android background service issues

There are repeatedly reported issues with some android devices not working in the background. Check if your device model is on dontkillmyapp list before you report new issue. For more information check out dontkillmyapp.com.

Another confusing fact about Android services is concept of foreground services. Foreground service in context of Android OS is different thing than background geolocation service of this plugin (they're related thought). Plugin's background geolocation service actually becomes foreground service when app is in the background. Confusing, right? :D

If service wants to continue to run in the background, it must "promote" itself to foreground service. Foreground services must have visible notification, which is the reason, why you can't disable drawer notification.

The notification can only be disabled, when app is running in the foreground, by setting config option startForeground: false (this is the default option), but will always be visible in the background (if service was started).

Recommend you to read https://developer.android.com/about/versions/oreo/bac


Cross-platform geolocation for Cordova / PhoneGap with battery-saving "circular region monitoring" and "stop detection".

This plugin can be used for geolocation when the app is running in the foreground or background. It is more battery and data efficient than html5 geolocation or cordova-geolocation plugin. It can be used side by side with other geolocation providers (eg. html5 navigator.geolocation).

You can choose from two location location providers:


See Which provider should I use? for more information about providers.

Example Application

Checkout repository cordova-plugin-background-geolocation-example.



Installing the plugin

cordova plugin add @mauron85/cordova-plugin-background-geolocation

You may also want to change default iOS permission prompts and set specific google play version and android support library version for compatibility with other plugins.

Note: Always consult documentation of other plugins to figure out compatible versions.

cordova plugin add @mauron85/cordova-plugin-background-geolocation \
  --variable ALWAYS_USAGE_DESCRIPTION="App requires ..." \
  --variable MOTION_USAGE_DESCRIPTION="App requires motion detection"

Or in config.xml:

<plugin name="cordova-plugin-background-geolocation" spec="@mauron85/cordova-plugin-background-geolocation@~3.1.0">
  <variable name="GOOGLE_PLAY_SERVICES_VERSION" value="11+" />
  <variable name="ANDROID_SUPPORT_LIBRARY_VERSION" value="26+" />
  <variable name="ICON" value="@mipmap/icon" />
  <variable name="SMALL_ICON" value="@mipmap/icon" />
  <variable name="ALWAYS_USAGE_DESCRIPTION" value="App requires background tracking " />
  <variable name="MOTION_USAGE_DESCRIPTION" value="App requires motion detection" /> 

Note: To apply changes, you must remove and reinstall plugin.

Registering plugin for Adobe® PhoneGap™ Build

This plugin should work with Adobe® PhoneGap™ Build without any modification. To register plugin add following line into your config.xml:

<plugin name="@mauron85/cordova-plugin-background-geolocation"/>

Note: If you're using hydration, you have to download and reinstall your app with every new version of the plugin, as plugins are not updated.



Plugin versionCordova CLICordova Platform AndroidCordova Platform iOS

Please note that as of Cordova Android 6.0.0 icons are by default in mipmap/ directory not drawable/ directory, so this plugin will have a build issue on < 6.0.0 Cordova builds, you will need to update Authenticator.xml to drawable directory from mipmap directory to work on older versions.


You will need to ensure that you have installed the following items through the Android SDK Manager:

Android SDK Tools26.0.2
Android SDK Platform-tools26.0.2
Android SDK Build-tools26.0.2
Android Support Repository47
Android Support Library26.1.0
Google Play Services11.8.0
Google Repository58

Android is no longer supporting downloading support libraries through the SDK Manager. The support libraries are now available through Google's Maven repository.


function onDeviceReady() {
    locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
    desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
    stationaryRadius: 50,
    distanceFilter: 50,
    notificationTitle: 'Background tracking',
    notificationText: 'enabled',
    debug: true,
    interval: 10000,
    fastestInterval: 5000,
    activitiesInterval: 10000,
    url: '',
    httpHeaders: {
      'X-FOO': 'bar'
    // customize post properties
    postTemplate: {
      lat: '@latitude',
      lon: '@longitude',
      foo: 'bar' // you can also add your own properties

  BackgroundGeolocation.on('location', function(location) {
    // handle your locations here
    // to perform long running operation on iOS
    // you need to create background task
    BackgroundGeolocation.startTask(function(taskKey) {
      // execute long running task
      // eg. ajax post location
      // IMPORTANT: task has to be ended by endTask

  BackgroundGeolocation.on('stationary', function(stationaryLocation) {
    // handle stationary locations here

  BackgroundGeolocation.on('error', function(error) {
    console.log('[ERROR] BackgroundGeolocation error:', error.code, error.message);

  BackgroundGeolocation.on('start', function() {
    console.log('[INFO] BackgroundGeolocation service has been started');

  BackgroundGeolocation.on('stop', function() {
    console.log('[INFO] BackgroundGeolocation service has been stopped');

  BackgroundGeolocation.on('authorization', function(status) {
    console.log('[INFO] BackgroundGeolocation authorization status: ' + status);
    if (status !== BackgroundGeolocation.AUTHORIZED) {
      // we need to set delay or otherwise alert may not be shown
      setTimeout(function() {
        var showSettings = confirm('App requires location tracking permission. Would you like to open app settings?');
        if (showSettings) {
          return BackgroundGeolocation.showAppSettings();
      }, 1000);

  BackgroundGeolocation.on('background', function() {
    console.log('[INFO] App is in background');
    // you can also reconfigure service (changes will be applied immediately)
    BackgroundGeolocation.configure({ debug: true });

  BackgroundGeolocation.on('foreground', function() {
    console.log('[INFO] App is in foreground');
    BackgroundGeolocation.configure({ debug: false });

  BackgroundGeolocation.on('abort_requested', function() {
    console.log('[INFO] Server responded with 285 Updates Not Required');

    // Here we can decide whether we want stop the updates or not.
    // If you've configured the server to return 285, then it means the server does not require further update.
    // So the normal thing to do here would be to `BackgroundGeolocation.stop()`.
    // But you might be counting on it to receive location updates in the UI, so you could just reconfigure and set `url` to null.

  BackgroundGeolocation.on('http_authorization', () => {
    console.log('[INFO] App needs to authorize the http requests');

  BackgroundGeolocation.checkStatus(function(status) {
    console.log('[INFO] BackgroundGeolocation service is running', status.isRunning);
    console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled);
    console.log('[INFO] BackgroundGeolocation auth status: ' + status.authorization);

    // you don't need to check status before start (this is just the example)
    if (!status.isRunning) {
      BackgroundGeolocation.start(); //triggers start on start event

  // you can also just start without checking for status
  // BackgroundGeolocation.start();

  // Don't forget to remove listeners at some point!
  // BackgroundGeolocation.removeAllListeners();

document.addEventListener('deviceready', onDeviceReady, false);


configure(options, success, fail)

Configure options:

locationProviderNumberallSet location provider @see PROVIDERSN/ADISTANCE_FILTER_PROVIDER
desiredAccuracyNumberallDesired accuracy in meters. Possible values [HIGH_ACCURACY, MEDIUM_ACCURACY, LOW_ACCURACY, PASSIVE_ACCURACY]. Accuracy has direct effect on power drain. Lower accuracy = lower power drain.allMEDIUM_ACCURACY
stationaryRadiusNumberallStationary radius in meters. When stopped, the minimum distance the device must move beyond the stationary location for aggressive background-tracking to engage.DIS50
debugBooleanallWhen enabled, the plugin will emit sounds for life-cycle events of background-geolocation! See debugging sounds table.allfalse
distanceFilterNumberallThe minimum distance (measured in meters) a device must move horizontally before an update event is generated. @see Apple docs.DIS,RAW500
stopOnTerminateBooleanallEnable this in order to force a stop() when the application terminated (e.g. on iOS, double-tap home button, swipe away the app).alltrue
startOnBootBooleanAndroidStart background service on device boot.allfalse
intervalNumberAndroidThe minimum time interval between location updates in milliseconds. @see Android docs for more information.all60000
fastestIntervalNumberAndroidFastest rate in milliseconds at which your app can handle location updates. @see Android docs.ACT120000
activitiesIntervalNumberAndroidRate in milliseconds at which activity recognition occurs. Larger values will result in fewer activity detections while improving battery life.ACT10000
stopOnStillActivityBooleanAndroid@deprecated stop location updates, when the STILL activity is detectedACTtrue
notificationsEnabledBooleanAndroidEnable/disable local notifications when tracking and syncing locationsalltrue
startForegroundBooleanAndroidAllow location sync service to run in foreground state. Foreground state also requires a notification to be presented to the user.allfalse
notificationTitleString optionalAndroidCustom notification title in the drawer. (goes with startForeground)all"Background tracking"
notificationTextString optionalAndroidCustom notification text in the drawer. (goes with startForeground)all"ENABLED"
notificationIconColorString optionalAndroidThe accent color to use for notification. Eg. #4CAF50. (goes with startForeground)all
notificationIconLargeString optionalAndroidThe filename of a custom notification icon. @see Android quirks. (goes with startForeground)all
notificationIconSmallString optionalAndroidThe filename of a custom notification icon. @see Android quirks. (goes with startForeground)all
activityTypeStringiOS[AutomotiveNavigation, OtherNavigation, Fitness, Other] Presumably, this affects iOS GPS algorithm. @see Apple docs for more informationall"OtherNavigation"
pauseLocationUpdatesBooleaniOSPauses location updates when app is paused. *@see Apple docsallfalse
saveBatteryOnBackgroundBooleaniOSSwitch to less accurate significant changes and region monitory when in backgroundallfalse
urlStringallServer url where to send HTTP POST with recorded locations @see HTTP locations postingall
syncUrlStringallServer url where to send fail to post locations @see HTTP locations postingall
syncThresholdNumberallSpecifies how many previously failed locations will be sent to server at onceall100
httpHeadersObjectallOptional HTTP headers sent along in HTTP requestall
maxLocationsNumberallLimit maximum number of locations stored into dball10000
postTemplateObject\|ArrayallCustomization post template @see Custom post templateall


Partial reconfiguration is possible by later providing a subset of the configuration options:

  debug: true

In this case new configuration options will be merged with stored configuration options and changes will be applied immediately.

Important: Because configuration options are applied partially, it's not possible to reset option to default value just by omitting it's key name and calling configure method. To reset configuration option to the default value, it's key must be set to null!

// Example: reset postTemplate to default
  postTemplate: null

getConfig(success, fail)

Platform: iOS, Android

Get current configuration. Method will return all configuration options and their values in success callback. Because configure method can be called with subset of the configuration options only, getConfig method can be used to check the actual applied configuration.

BackgroundGeolocation.getConfig(function(config) {


Platform: iOS, Android

Start background geolocation.


Platform: iOS, Android

Stop background geolocation.

getCurrentLocation(success, fail, options)

Platform: iOS, Android

One time location check to get current location of the device.

Option parameterTypeDescription
timeoutNumberMaximum time in milliseconds device will wait for location
maximumAgeNumberMaximum age in milliseconds of a possible cached location that is acceptable to return
enableHighAccuracyBooleanif true and if the device is able to provide a more accurate position, it will do so
Success callback parameterTypeDescription
locationObjectlocation object (@see Location event)
Error callback parameterTypeDescription
codeNumberReason of an error occurring when using the geolocating device
messageStringMessage describing the details of the error

Error codes:

ValueAssociated constantDescription
1PERMISSION_DENIEDRequest failed due missing permissions
2LOCATION_UNAVAILABLEInternal source of location returned an internal error
3TIMEOUTTimeout defined by `option.timeout was exceeded

isLocationEnabled(success, fail)

Deprecated: This method is deprecated and will be removed in next major version. Use checkStatus as replacement.

Platform: iOS, Android

One time check for status of location services. In case of error, fail callback will be executed.

Success callback parameterTypeDescription
enabledBooleantrue/false (true when location services are enabled)

checkStatus(success, fail)

Check status of the service

Success callback parameterTypeDescription
isRunningBooleantrue/false (true if service is running)
locationServicesEnabledBooleantrue/false (true if location services are enabled)
authorizationNumberauthorization status

Authorization statuses:

  • AUTHORIZED - authorization to run in background and foreground
  • AUTHORIZED_FOREGROUND iOS only authorization to run in foreground only

Note: In the Android concept of authorization, these represent application permissions.


Platform: Android >= 6, iOS >= 8.0

Show app settings to allow change of app location permissions.


Platform: Android

Show system settings to allow configuration of current location sources.

getLocations(success, fail)

Platform: iOS, Android

Method will return all stored locations. This method is useful for initial rendering of user location on a map just after application launch.

Success callback parameterTypeDescription
locationsArraycollection of stored locations
  function (locations) {

getValidLocations(success, fail)

Platform: iOS, Android

Method will return locations which have not yet been posted to server.

Success callback parameterTypeDescription
locationsArraycollection of stored locations

deleteLocation(locationId, success, fail)

Platform: iOS, Android

Delete location with locationId.

deleteAllLocations(success, fail)

Note: You don't need to delete all locations. The plugin manages the number of stored locations automatically and the total count never exceeds the number as defined by option.maxLocations.

Platform: iOS, Android

Delete all stored locations.

Note: Locations are not actually deleted from database to avoid gaps in locationId numbering. Instead locations are marked as deleted. Locations marked as deleted will not appear in output of BackgroundGeolocation.getValidLocations.

switchMode(modeId, success, fail)

Platform: iOS

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

In FOREGROUND mode the 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.

// switch to FOREGROUND mode

// switch to BACKGROUND mode


Platform: Android, iOS

Force sync of pending locations. Option syncThreshold will be ignored and all pending locations will be immediately posted to syncUrl in single batch.

getLogEntries(limit, fromId, minLevel, success, fail)

Platform: Android, iOS

Return all logged events. Useful for plugin debugging.

limitNumberlimits number of returned entries
fromIdNumberreturn entries after fromId. Useful if you plan to implement infinite log scrolling*
minLevelStringreturn log entries above level. Available levels: ["TRACE", "DEBUG", "INFO", "WARN", "ERROR]
successFunctioncallback function which will be called with log entries

*Example of infinite log scrolling

Format of log entry:

idNumberid of log entry as stored in db
timestampNumbertimestamp in milliseconds since beginning of UNIX epoch
levelStringlog level
messageStringlog message
stackTraceStringrecorded stacktrace (Android only, on iOS part of message)


Unregister all event listeners for given event. If parameter event is not provided then all event listeners will be removed.


NameCallback paramPlatformProvider*Description
locationLocationallallon location update
stationaryLocationallDIS,ACTon device entered stationary mode
activityActivityAndroidACTon activity detection
error{ code, message }allallon plugin error
authorizationstatusallallon user toggle location service
startallallgeolocation has been started
stopallallgeolocation has been stopped
foregroundAndroidallapp entered foreground state (visible)
backgroundAndroidallapp entered background state
abort_requestedallallserver responded with "285 Updates Not Required"
http_authorizationallallserver responded with "401 Unauthorized"

Location event

Location parameterTypeDescription
idNumberID of location as stored in DB (or null)
providerStringgps, network, passive or fused
locationProviderNumberlocation provider
timeNumberUTC time of this fix, in milliseconds since January 1, 1970.
latitudeNumberLatitude, in degrees.
longitudeNumberLongitude, in degrees.
accuracyNumberEstimated accuracy of this location, in meters.
speedNumberSpeed if it is available, in meters/second over ground.
altitudeNumberAltitude if available, in meters above the WGS 84 reference ellipsoid.
bearingNumberBearing, in degrees.
isFromMockProviderBoolean(android only) True if location was recorded by mock provider
mockLocationsEnabledBoolean(android only) True if device has mock locations enabled

Locations parameters isFromMockProvider and mockLocationsEnabled are not posted to url or syncUrl by default. Both can be requested via option postTemplate.

Note: Do not use location id as unique key in your database as ids will be reused when option.maxLocations is reached.

Activity event

Activity parameterTypeDescription
confidenceNumberPercentage indicating the likelihood user is performing this activity.

Event listeners can registered with:

const eventSubscription = BackgroundGeolocation.on('event', callbackFn);

And unregistered:


Note: Components should unregister all event listeners in componentWillUnmount method, individually, or with removeAllListeners

HTTP locations posting

All locations updates are recorded in the local db at all times. When the App is in foreground or background, in addition to storing location in local db, the location callback function is triggered. The number of locations stored in db is limited by option.maxLocations and never exceeds this number. Instead, old locations are replaced by new ones.

When option.url is defined, each location is also immediately posted to url defined by option.url. If the post is successful, the location is marked as deleted in local db.

When option.syncUrl is defined, all locations that fail to post locations will be coalesced and sent in some time later in a one single batch. Batch sync takes place only when the number of failed-to-post locations reaches option.syncTreshold. Locations are sent only in single batch, when the number of locations reaches option.syncTreshold. (No individual locations will be sent)

The request body of posted locations is always an array, even when only one location is sent.

Warning: option.maxLocations has to be larger than option.syncThreshold. It's recommended to be 2x larger. In any other case the location syncing might not work properly.

Custom post template

With option.postTemplate it is possible to specify which location properties should be posted to option.url or option.syncUrl. This can be useful to reduce the number of bytes sent "over the wire".

All wanted location properties have to be prefixed with @. For all available properties check Location event.

Two forms are supported:


  postTemplate: {
    lat: '@latitude',
    lon: '@longitude',
    foo: 'bar' // you can also add your own properties


  postTemplate: ['@latitude', '@longitude', 'foo', 'bar']

Note: Keep in mind that all locations (even a single one) will be sent as an array of object(s), when postTemplate is jsonObject and array of array(s) for jsonArray!

Android Headless Task (Experimental)

A special task that gets executed when the app is terminated, but the plugin was configured to continue running in the background (option stopOnTerminate: false). In this scenario the Activity was killed by the system and all registered event listeners will not be triggered until the app is relaunched.

Note: Prefer configuration options url and syncUrl over headless task. Use it sparingly!

Task event

event.nameStringName of the event [ "location", "stationary", "activity" ]
event.paramsObjectEvent parameters. @see Events

Keep in mind that the callback function lives in an isolated scope. Variables from a higher scope cannot be referenced!

Following example requires CORS enabled backend server.

BackgroundGeolocation.headlessTask(function(event) {
    if (event.name === 'location' ||
      event.name === 'stationary') {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '');
        xhr.setRequestHeader('Content-Type', 'application/json');

    return 'Processing event: ' + event.name; // will be logged

Example of backend server

Background-geolocation-server is a backend server written in nodejs with CORS - Cross-Origin Resource Sharing support. There are instructions how to run it and simulate locations on Android, iOS Simulator and Genymotion.



On iOS the plugin will execute your registered .on('location', callbackFn) callback function. You may manually POST the received Geolocation to your server using standard XHR. However for long running tasks, you need to wrap your code in startTask - endTask block.

stationaryRadius (apply only for DISTANCE_FILTER_PROVIDER)

The plugin uses iOS Significant Changes API, and starts triggering callbackFn only when a cell-tower switch is detected (i.e. the device exits stationary radius). The function switchMode is provided to force the plugin to enter "BACKGROUND" stationary or "FOREGROUND" mode.

Plugin cannot detect the exact moment the device moves out of the stationary-radius. In normal conditions, it can take as much as 3 city-blocks to 1/2 km before stationary-region exit is detected.


On Android devices it is recommended to have a notification in the drawer (option startForeground:true). This gives plugin location service higher priority, decreasing probability of OS killing it. Check wiki for explanation.

Custom ROMs

Plugin should work with custom ROMS at least DISTANCE_FILTER_PROVIDER. But ACTIVITY_PROVIDER provider depends on Google Play Services. Usually ROMs don't include Google Play Services libraries. Strange bugs may occur, like no GPS locations (only from network and passive) and other. When posting issue report, please mention that you're using custom ROM.


Note: Following section was kindly copied from phonegap-plugin-push. Visit link for resolving issue with facebook plugin.

If you have an issue compiling the app and you're getting an error similar to this (com.android.dex.DexException: Multiple dex files define):

com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
  at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
  at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
  at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
  at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
  at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
  at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
  at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
  at com.android.dx.command.dexer.Main.run(Main.java:277)
  at com.android.dx.command.dexer.Main.main(Main.java:245)
  at com.android.dx.command.Main.main(Main.java:106)

Then at least one other plugin you have installed is using an outdated way to declare dependencies such as android-support or play-services-gcm. This causes gradle to fail, and you'll need to identify which plugin is causing it and request an update to the plugin author, so that it uses the proper way to declare dependencies for cordova. See this for the reference on the cordova plugin specification, it'll be usefull to mention it when creating an issue or requesting that plugin to be updated.

Common plugins to suffer from this outdated dependency management are plugins related to facebook, google+, notifications, crosswalk and google maps.

Android Permissions

Android 6.0 "Marshmallow" introduced a new permissions model where the user can turn on and off permissions as necessary. When user disallow location access permissions, error configure callback will be called with error code: 2.

Notification icons

Note: Only available for API Level >=21.

To use custom notification icons, you need to put icons into res/drawable directory of your app. You can automate the process as part of after_platform_add hook configured via config.xml. Check config.xml and scripts/res_android.js of example app for reference.

If you only want a single large icon, set notificationIconLarge to null and include your icon's filename in the notificationIconSmall parameter.

With Adobe® PhoneGap™ Build icons must be placed into locales/android/drawable dir at the root of your project. For more information go to how-to-add-native-image-with-phonegap-build.

Intel XDK

Plugin will not work in XDK emulator ('Unimplemented API Emulation: BackgroundGeolocation.start' in emulator). But will work on real device.




There is nice cordova plugin cordova-plugin-geofence, which does exactly that. Let's keep this plugin lightweight as much as possible.




Apache License

Copyright (c) 2013 Christopher 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.


Rate & Review

Great Documentation0
Easy to Use0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Unwelcoming Community0
No reviews found
Be the first to rate


No alternatives found


No tutorials found
Add a tutorial