Search code examples
cordovaionic-frameworkgeolocationmapscordova-plugins

Ionic: background geolocation


I'm using Ionic & Firebase to create an Android application for geolocation. I used ngCordova.geolocation and I successfully get the device current location.

My problem is that I need to continuously get current location, as long as the user doesn't turn off geolocation and callback a function every time the geolocation change, and test if the user is inside one of many circles in the maps.


Solution

  • Since you are already using the ngcordova.geolocation plugin, it has a method to keep tracking the device position via watchPosition. There's an example in the docs indicating how to use it:

      var watchOptions = {
        timeout : 3000,
        enableHighAccuracy: false // may cause errors if true
      };
    
      var watch = $cordovaGeolocation.watchPosition(watchOptions);
      watch.then(
        null,
        function(err) {
          // error
        },
        function(position) {
          var lat  = position.coords.latitude
          var long = position.coords.longitude
    
          /* Further code to execute on each update */
      });