Cordova geolocation watchPosition frequency is higher than the options allow it

11,151

Updated in light of comments below

There is no frequency parameter available in the geolocation options for watchPosition() hence any value you pass will be ignored. The success callback registered via watchPosition() is invoked each time the native location manager receives a position update from the GPS hardware (in the case of enableHighAccuracy=true) so it's not called on a fixed interval.

The native location managers (both Android and iOS) are event-driven, i.e. they receive updates from the GPS hardware as and when it delivers them at a non-fixed interval. Hence trying to apply a fixed frequency to this is trying to fit a square peg in a round hole - you cannot demand that the GPS hardware deliver you a location update exactly ever N seconds.

While you can call getCurrentPosition() on a fixed interval, this method simply returns last received position or requests a new one.

If the problem is that the updates are too frequent, you could log the time each update is received at, and only accept the next update after N seconds, e.g.

var lastUpdateTime,
minFrequency = 10*1000,
watchOptions = {
    timeout : 60*60*1000,
    maxAge: 0,
    enableHighAccuracy: true
};

function on_success(position){
    var now = new Date();
    if(lastUpdateTime && now.getTime() - lastUpdateTime.getTime() < minFrequency){
        console.log("Ignoring position update");
        return;
    }
    lastUpdateTime = now;

    // do something with position
}
navigator.geolocation.watchPosition(on_success,on_error,watchOptions);

This, however, will not stop the device requesting updates more frequently, hence consuming a relatively large amount of battery.

The native Android LocationManager does allow you to specify a minimum time between updates when requesting location in order to minimise battery drain, however cordova-plugin-geolocation on Android doesn't implement use LocationManager directly, but instead uses the W3C Geolocation API Specification in the native webview, which does not allow you to specify this.

However, you can use this plugin to do this: cordova-plugin-locationservices

It will allow you to specify:

interval: Set the desired interval for active location updates, in milliseconds.

fastInterval: Explicitly set the fastest interval for location updates, in milliseconds.

Share:
11,151
Kingalione
Author by

Kingalione

Full stack developer, loves swift and node.js

Updated on June 16, 2022

Comments

  • Kingalione
    Kingalione almost 2 years

    in my ionic/angularjs application I'm using the geolocation plugin: https://github.com/apache/cordova-plugin-geolocation

    Like in the documentation I use this to configure the watch:

    var watchOptions = {
        frequency : 10*1000,
        timeout : 60*60*1000,
        enableHighAccuracy: true // may cause errors if true
    };
    
    watch = navigator.geolocation.watchPosition(on_success,on_error,watchOptions);
    

    But however on android the frequency is much higher than 10 seconds (about 0.5 Seconds). On iOS it works great. What is the problem here?

  • cr8ivecodesmith
    cr8ivecodesmith about 7 years
    Is there a counterpart for cordova-plugin-locationservices for iOS? A workaround I can think of is to create your own wrapper around the default watchPosition where you call the watchPosition and clearWatch depending on the frequency value you give it.
  • Douglas Timms
    Douglas Timms about 7 years
    Thanks! However, in your sample code, I think one line is incorrect and should instead be: if(lastUpdateTime && now.getTime() - lastUpdateTime.getTime() < minFrequency){