node package manager
Orgs are free. Discover, share, and reuse code in your team. Create a free org »

cordova-background-geolocation-lt

Cordova Background Geolocation · npm npm

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.

The plugin's Philosophy of Operation is to use motion-detection APIs (using accelerometer, gyroscope and magnetometer) to detect when the device is moving and stationary.

  • When the device is detected to be moving, the plugin will automatically start recording a location according to the configured distanceFilter (meters).

  • When the device is detected be stationary, the plugin will automatically turn off location-services to conserve energy.

Also available for React Native, NativeScript and pure native apps.


The Android plugin requires purchasing a license. However, it will work for DEBUG builds. It will not work with RELEASE builds without purchasing a license.

(2017) This plugin is supported full-time and field-tested daily since 2013.


Home Settings

Contents

🔷 Installing the plugin

From npm

$ cordova plugin add cordova-background-geolocation-lt

Phonegap Build

  <plugin name="cordova-background-geolocation-lt" source="npm">
 
  </plugin>

From master (latest, greatest.)

$ cordova plugin add https://github.com/transistorsoft/cordova-background-geolocation-lt.git

🔷 Android SDK

If building from your local machine (as you should be), ensure you have the following items installed or updated in Android SDK Manager

SDK Tools

SDK Platforms

🔷 Using the Plugin

The plugin creates the object window.BackgroundGeolocation. See API Documentation for details

Ionic 2+ and Typescript

Sample Implementation

platform.ready().then(() => {
  let bgGeo = (<any>window).BackgroundGeolocation;
});
 

#configure the Plugin

There are three simple steps to using BackgroundGeolocation:

  1. Listen to events
  2. #configure the plugin
  3. #start the plugin
// 1.  Listen to events
bgGeo.on('location', onLocation, onLocationFailure);
bgGeo.on('motionchange', onMotionChange);
bgGeo.on('providerchange', onProviderChange);
 
// 2. Configure the plugin.  
bgGeo.configure({
  desiredAccuracy: 0,   // <-- Config params
  distanceFilter: 50
}, function(state) {    // <-- Current state provided to #configure callback
  // 3.  Start tracking
  console.log('BackgroundGeolocation is configured and ready to use');
  if (!state.enabled) {
    bgGeo.start(function() {
      console.log('- BackgroundGeolocation tracking started');
    });
  }
});
// NOTE:  Do NOT execute any API methods until the callback to #configure
// method above executes!
// For example, do not do this here:
// bgGeo.getCurrentPosition()   // <-- NO!
// bgGeo.getState();            // <-- NO!

⚠️ Do not execute any API method (aside from #getState or adding event-listeners with #on) before the callbackFn to the #configure method fires, as noted above.

🔷 Example

 
////
// As with all Cordova plugins, you must configure within an #deviceready callback.
//
function onDeviceReady() {
    // Get a reference to the plugin.
    var bgGeo = window.BackgroundGeolocation;
 
    //This callback will be executed every time a geolocation is recorded in the background.
    var callbackFn = function(location) {
        var coords = location.coords;
        var lat    = coords.latitude;
        var lng    = coords.longitude;
        console.log('- Location: ', JSON.stringify(location));        
    };
 
    // This callback will be executed if a location-error occurs.  Eg: this will be called if user disables location-services.
    var failureFn = function(errorCode) {
        console.warn('- BackgroundGeoLocation error: ', errorCode);
    }
 
    // Listen to location events & errors.
    bgGeo.on('location', callbackFn, failureFn);
    // Fired whenever state changes from moving->stationary or vice-versa.
    bgGeo.on('motionchange', function(isMoving) {
      console.log('- onMotionChange: ', isMoving);
    });
    // Fired whenever a geofence transition occurs.
    bgGeo.on('geofence', function(geofence) {
      console.log('- onGeofence: ', geofence.identifier, geofence.location);
    });
    // Fired whenever an HTTP response is received from your server.
    bgGeo.on('http', function(response) {
      console.log('http success: ', response.responseText);
    }, function(response) {
      console.log('http failure: ', response.status);
    });
 
    // BackgroundGeoLocation is highly configurable.
    bgGeo.configure({
        // Geolocation config
        desiredAccuracy: 0,
        distanceFilter: 10,
        stationaryRadius: 25,
        // Activity Recognition config
        activityRecognitionInterval: 10000,
        stopTimeout: 5,
        // Application config
        debug: true,  // <-- Debug sounds & notifications.
        stopOnTerminate: false,
        startOnBoot: true,
        // HTTP / SQLite config
        url: "http://your.server.com/locations",
        method: "POST",
        autoSync: true,
        maxDaysToPersist: 3,
        headers: {  // <-- Optional HTTP headers
            "X-FOO": "bar"
        },
        params: {   // <-- Optional HTTP params
            "auth_token": "maybe_your_server_authenticates_via_token_YES?"
        }
    }, function(state) {
        // This callback is executed when the plugin is ready to use.
        console.log("BackgroundGeolocation ready: ", state);
        if (!state.enabled) {
            bgGeo.start();
        }
    });
 
    // The plugin is typically toggled with some button on your UI.
    function onToggleEnabled(value) {
        if (value) {
            bgGeo.start();
        } else {
            bgGeo.stop();
        }
    }
}
 

🔷 Configuring the plugin

After adding the plugin, your config.xml will contain the following block, containing a number of <variable /> for you to configure (eg: LICENSE):

<widget id="com.your.company.app.id">
  <plugin name="cordova-background-geolocation">
    <variable name="LICENSE" value="YOUR_LICENSE_KEY" />
    <variable name="GOOGLE_API_VERSION" value="11.6.0" />
    <variable name="APPCOMPAT_VERSION" value="26.1.0" />
    <variable name="LOCATION_ALWAYS_AND_WHEN_IN_USE_USAGE_DESCRIPTION" value="Always use is required for background location tracking" />
    <variable name="LOCATION_ALWAYS_USAGE_DESCRIPTION" value="Background location-tracking is required" />
    <variable name="LOCATION_WHEN_IN_USE_USAGE_DESCRIPTION" value="Background location-tracking is required" />
    <variable name="MOTION_USAGE_DESCRIPTION" value="Using the accelerometer increases battery-efficiency by intelligently toggling location-tracking only when the device is detected to be moving" />
  </plugin>
  .
  .
  .
</widget>

To configure these <variable />, you simply re-add the plugin, providing desired --variable to the cordova plugin add command. You can combine multiple --variable together in one command or execute cordova plugin add successivly.

$ cordova plugin add cordova-background-geolocation-lt --variable VARIABLE_NAME=value

$ cordova platform remove android
$ cordova platform add android

❗️ To apply changes to these <variable />, you must remove/re-add the cordova platform(s)

Configuration Variables

Name Default Description
🔵 iOS
LOCATION_ALWAYS_AND_WHEN_IN_USE_USAGE_DESCRIPTION "CHANGEME" [iOS 11+] Customize the message displayed to the user when AlwaysAndWhenInUse location authorization is requested. This variable is added to your iOS .plist
LOCATION_WHEN_IN_USE_USAGE_DESCRIPTION "CHANGEME" Customize the message displayed to the user when WhenInUse location authorization is requested. This variable is added to your iOS .plist
LOCATION_ALWAYS_USAGE_DESCRIPTION "CHANGEME" [deprecated in 11.0] Customize the message displayed to the user when Always location authorization is requested. This variable is added to your iOS .plist
MOTION_USAGE_DESCRIPTION "CHANGEME" Customize the message displayed to the user when "Motion & Fitness" permission is requested. The plugin is highly optimized to use iOS CMMotionActivityManager API for intelligently toggling location-services only when the plugin is detected to be moving / stationary.
BACKGROUND_MODE_LOCATION "location" Adds the iOS background-mode location to your iOS .plist file. This is the default behaviour. To disable this, (ie: for those using useSignificantChangesOnly), provide an empty-string. ⚠️ If you do want the default behaviour of background-location updates, simply IGNORE this variable -- Do NOT even provide it.
🔵 Android
LICENSE "" Your Android license key generated from Product Dashboard
GOOGLE_API_VERSION 11.6.0 Sets the desired version of play-services-location dependency. Minimum 11.2.0 is required. Many other plugins require play-services dependencies, (eg: cordova-plugin-googlemaps, phonegap-plugin-push): If the version of play-services and/or firebase is not aligned to the same version for ALL plugins, your build will fail.
APPCOMPAT_VERSION 26.1.0 Sets the desired version of com.google.android.appcompat-v7 dependency. Many other plugins can require a different version of appcompat-v7 dependeny: If the version of appcompat-v7 is not aligned to the same version for ALL plugins, your build will fail.

Configuring Multiple Variables at Once

Use the \ character followed by [ENTER] in console to configure multiple variables with one command:

$ cordova plugin add cordova-background-geolocation-lt \
  --variable LICENSE=your_key_here \
  --variable GOOGLE_API_VERSION=11.6.0 \
  --variable APPCOMPAT_VERSION=27.0.0 \
  --variable MOTION_USAGE_DESCRIPTION="My motion usage description"
 
$ cordova platform remove android
$ cordova platform add android
 
$ cordova platform remove ios
$ cordova platform add ios

Configuring your LICENSE key

After generating your Android license-key in the Product Dashboard, Configure the LICENSE variable:

$ cordova plugin add cordova-background-geolocation-lt --variable LICENSE=your_key_here
 
// Remove / re-add the Android platform
$ cordova platform remove android
$ cordova platform add android

Configuring play-services Version

Many other plugins require Google Play Services and/or Firebase libraries. This is a common source of Android build-failures, since the play-services library version must be aligned to the same version for all plugins. For example, when one plugin imports version 11.0.1 and another one imports 11.2.0, a gradle build failure will occur. Use the GOOGLE_API_VERSION to align the required play-services version with other plugins.

⚠️ The plugin requires a minimum version of 11.2.0.

$ cordova plugin add cordova-background-geolocation-lt --variable GOOGLE_API_VERSION=11.2.0
$ cordova platform remove android
$ cordova platform add android

Configuring for useSignificantChangesOnly

For those using useSignificantChangesOnly: true, possibly because Apple denied your use of the background location capability, you can disable background location by providing the BACKGROUND_MODE_LOCATION <variable /> with an empty-string:

$ cordova plugin add cordova-background-geolocation-lt --variable BACKGROUND_MODE_LOCATION=""
 
$ cordova platform remove ios
$ cordova platform add ios

🔷 Advanced Sample Application

A fully-featured SampleApp is available in its own public repo. After first cloning that repo, follow the installation instructions in the README there. This SampleApp includes a settings-screen allowing you to quickly experiment with all the different settings available for each platform.

If you're using XCode, boot the SampleApp in the iOS Simulator and enable Debug->Location->Freeway Drive.

🔷 Simple Testing Server

A simple Node-based web-application with SQLite database is available for field-testing and performance analysis. If you're familiar with Node, you can have this server up-and-running in about one minute.

Licence

cordova-background-geolocation
Copyright (c) 2017, Transistor Software (9224-2932 Quebec Inc)
All rights reserved.
sales@transistorsoft.com
http://transistorsoft.com
  1. Preamble: This Agreement governs the relationship between YOU OR THE ORGANIZATION ON WHOSE BEHALF YOU ARE ENTERING INTO THIS AGREEMENT (hereinafter: Licensee) and Transistor Software, a LICENSOR AFFILIATION whose principal place of business is Montreal, Quebec, Canada (Hereinafter: Licensor). This Agreement sets the terms, rights, restrictions and obligations on using {software} created and owned by Licensor, as detailed herein

  2. License Grant: Licensor hereby grants Licensee a Personal, Non-assignable & non-transferable, Commercial, Royalty free, Including the rights to create but not distribute derivative works, Non-exclusive license, all with accordance with the terms set forth and other legal restrictions set forth in 3rd party software used while running Software.

    2.1 Limited: Licensee may use Software for the purpose of: - Running Software on Licensee's Website[s] and Server[s]; - Allowing 3rd Parties to run Software on Licensee's Website[s] and Server[s]; - Publishing Software’s output to Licensee and 3rd Parties; - Distribute verbatim copies of Software's output (including compiled binaries); - Modify Software to suit Licensee’s needs and specifications.

    2.2 Binary Restricted: Licensee may sublicense Software as a part of a larger work containing more than Software, distributed solely in Object or Binary form under a personal, non-sublicensable, limited license. Such redistribution shall be limited to unlimited codebases.

  3. 2.3 Non Assignable & Non-Transferable: Licensee may not assign or transfer his rights and duties under this license.

    2.4 Commercial, Royalty Free: Licensee may use Software for any purpose, including paid-services, without any royalties

    2.5 Including the Right to Create Derivative Works: Licensee may create derivative works based on Software, including amending Software’s source code, modifying it, integrating it into a larger work or removing portions of Software, as long as no distribution of the derivative works is made.

  4. Term & Termination: The Term of this license shall be until terminated. Licensor may terminate this Agreement, including Licensee's license in the case where Licensee :

    3.1 became insolvent or otherwise entered into any liquidation process; or

    3.2 exported The Software to any jurisdiction where licensor may not enforce his rights under this agreements in; or

    3.3 Licensee was in breach of any of this license's terms and conditions and such breach was not cured, immediately upon notification; or

    3.4 Licensee in breach of any of the terms of clause 2 to this license; or

    3.5 Licensee otherwise entered into any arrangement which caused Licensor to be unable to enforce his rights under this License.

  5. Payment: In consideration of the License granted under clause 2, Licensee shall pay Licensor a FEE, via Credit-Card, PayPal or any other mean which Licensor may deem adequate. Failure to perform payment shall construe as material breach of this Agreement.

  6. Upgrades, Updates and Fixes: Licensor may provide Licensee, from time to time, with Upgrades, Updates or Fixes, as detailed herein and according to his sole discretion. Licensee hereby warrants to keep The Software up-to-date and install all relevant updates and fixes, and may, at his sole discretion, purchase upgrades, according to the rates set by Licensor. Licensor shall provide any update or Fix free of charge; however, nothing in this Agreement shall require Licensor to provide Updates or Fixes.

    5.1 Upgrades: for the purpose of this license, an Upgrade shall be a material amendment in The Software, which contains new features and or major performance improvements and shall be marked as a new version number. For example, should Licensee purchase The Software under version 1.X.X, an upgrade shall commence under number 2.0.0.

    5.2 Updates: for the purpose of this license, an update shall be a minor amendment in The Software, which may contain new features or minor improvements and shall be marked as a new sub-version number. For example, should Licensee purchase The Software under version 1.1.X, an upgrade shall commence under number 1.2.0.

    5.3 Fix: for the purpose of this license, a fix shall be a minor amendment in The Software, intended to remove bugs or alter minor features which impair the The Software's functionality. A fix shall be marked as a new sub-sub-version number. For example, should Licensee purchase Software under version 1.1.1, an upgrade shall commence under number 1.1.2.

  7. Support: Software is provided under an AS-IS basis and without any support, updates or maintenance. Nothing in this Agreement shall require Licensor to provide Licensee with support or fixes to any bug, failure, mis-performance or other defect in The Software.

    6.1 Bug Notification: Licensee may provide Licensor of details regarding any bug, defect or failure in The Software promptly and with no delay from such event; Licensee shall comply with Licensor's request for information regarding bugs, defects or failures and furnish him with information, screenshots and try to reproduce such bugs, defects or failures.

    6.2 Feature Request: Licensee may request additional features in Software, provided, however, that (i) Licensee shall waive any claim or right in such feature should feature be developed by Licensor; (ii) Licensee shall be prohibited from developing the feature, or disclose such feature request, or feature, to any 3rd party directly competing with Licensor or any 3rd party which may be, following the development of such feature, in direct competition with Licensor; (iii) Licensee warrants that feature does not infringe any 3rd party patent, trademark, trade-secret or any other intellectual property right; and (iv) Licensee developed, envisioned or created the feature solely by himself.

  8. Liability: To the extent permitted under Law, The Software is provided under an AS-IS basis. Licensor shall never, and without any limit, be liable for any damage, cost, expense or any other payment incurred by Licensee as a result of Software’s actions, failure, bugs and/or any other interaction between The Software  and Licensee’s end-equipment, computers, other software or any 3rd party, end-equipment, computer or services. Moreover, Licensor shall never be liable for any defect in source code written by Licensee when relying on The Software or using The Software’s source code.

  9. Warranty:

    8.1 Intellectual Property: Licensor hereby warrants that The Software does not violate or infringe any 3rd party claims in regards to intellectual property, patents and/or trademarks and that to the best of its knowledge no legal action has been taken against it for any infringement or violation of any 3rd party intellectual property rights.

    8.2 No-Warranty: The Software is provided without any warranty; Licensor hereby disclaims any warranty that The Software shall be error free, without defects or code which may cause damage to Licensee’s computers or to Licensee, and that Software shall be functional. Licensee shall be solely liable to any damage, defect or loss incurred as a result of operating software and undertake the risks contained in running The Software on License’s Server[s] and Website[s].

    8.3 Prior Inspection: Licensee hereby states that he inspected The Software thoroughly and found it satisfactory and adequate to his needs, that it does not interfere with his regular operation and that it does meet the standards and scope of his computer systems and architecture. Licensee found that The Software interacts with his development, website and server environment and that it does not infringe any of End User License Agreement of any software Licensee may use in performing his services. Licensee hereby waives any claims regarding The Software's incompatibility, performance, results and features, and warrants that he inspected the The Software.

  10. No Refunds: Licensee warrants that he inspected The Software according to clause 7(c) and that it is adequate to his needs. Accordingly, as The Software is intangible goods, Licensee shall not be, ever, entitled to any refund, rebate, compensation or restitution for any reason whatsoever, even if The Software contains material flaws.

  11. Indemnification: Licensee hereby warrants to hold Licensor harmless and indemnify Licensor for any lawsuit brought against it in regards to Licensee’s use of The Software in means that violate, breach or otherwise circumvent this license, Licensor's intellectual property rights or Licensor's title in The Software. Licensor shall promptly notify Licensee in case of such legal action and request Licensee's consent prior to any settlement in relation to such lawsuit or claim.

  12. Governing Law, Jurisdiction: Licensee hereby agrees not to initiate class-action lawsuits against Licensor in relation to this license and to compensate Licensor for any legal fees, cost or attorney fees should any claim brought by Licensee against Licensor be denied, in part or in full.