Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To implement background geolocation in an Ionic app using ngCordova, you can follow the below steps:

  1. Install the Cordova plugin for background geolocation:
cordova plugin add cordova-plugin-mauron85-background-geolocation
  1. Install the ngCordova module:
bower install ngCordova
  1. Add the ngCordova module and the Cordova plugin to your app's module:
angular.module('app', ['ngCordova', 'cordova.plugins.backgroundGeolocation'])
  1. Create a controller to handle background geolocation:
angular.module('app').controller('LocationCtrl', function($cordovaBackgroundGeolocation) {

  var options = {
    desiredAccuracy: 10,
    stationaryRadius: 50,
    distanceFilter: 50,
    debug: false,
    notificationTitle: 'Background tracking',
    notificationText: 'ENABLED',
    interval: 10000,
    fastestInterval: 5000,
    activitiesInterval: 10000,
    stopOnTerminate: false,
    startOnBoot: true
  };

  $cordovaBackgroundGeolocation.configure(options).then(function(result) {
    console.log('Background geolocation plugin configured');
  }, function(err) {
    console.log('Error configuring background geolocation plugin: ' + err);
  });

  $cordovaBackgroundGeolocation.start();

});
  1. Add the LocationCtrl to your view and test it on a device.

This code will configure and start the background geolocation plugin, which will track the device's location even when the app is in the background or the device is locked. You can use the plugin's events and methods to further customize the behavior and handle the location data.