htmlgpsw3c-geolocation

Reduce the frequency of geolocation watchPosition calls


I'm writing a tracking app for a logistics company, which needs to report the location of each phone back to base on a regular basis. The geolocation.watchPosition method seems ideal, except that as far as I can tell it updates the location as often as once per second with seemingly no way to increase the interval between callbacks. Once per second is a little too often from a data usage point of view - once per minute would be ideal.

Is there an option I can pass to watchPosition to make it execute the callback with a minimum interval?


Solution

  • You could allow it to still call watchPosition once per second, but then separate out the reporting back to base into a separate callback that is run once per minute on a timer.

    e.g.:

    var GPSStore = {};
    navigator.geolocation.watchPosition(
      function(current_location) {
        // Just store the latest co-ordinates in memory as often as they are generated
        GPSStore.location = current_location;
      }
    );
    // Once a minute transmit the latest co-ordinates
    setInterval(function() {
      transmitLocation(GPSStore.location);
    }, 1000*60);