Phonegap, Cordova watchposition fire success every

2020-06-21 07:02发布

Platform: iOS6/OSx Lion.

I'm trying to puzzle out the way Phonegap/Cordova work with navigator.geolocation.watchPosition.

The docs says that the option "maximumAge" is the one that ask the system for retrieve the position.

So with these options:

{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }

I espect the position request will be fired every 3 seconds?

And no matter what maximumAge I put the success is fired every 1 second...

Anyone can explain please?

Thanks Bye
Rob

1条回答
看我几分像从前
2楼-- · 2020-06-21 07:26

I am currently working around this issue by using getCurrentPosition with a setInterval. I'm not sure what the consequences may be, but this seems to give me the most control and appears to be the most consistent method across platforms.

// call this once
setupWatch(3000);

// sets up the interval at the specified frequency
function setupWatch(freq) {
    // global var here so it can be cleared on logout (or whenever).
    activeWatch = setInterval(watchLocation, freq);
}

// this is what gets called on the interval.
function watchLocation() {
    var gcp = navigator.geolocation.getCurrentPosition(
            updateUserLoc, onLocationError, {
                enableHighAccuracy: true
            });


    // console.log(gcp);

}

// do something with the results

function updateUserLoc(position) {


var location = {
    lat : position.coords.latitude,
    lng : position.coords.longitude
};

console.log(location.lat);
console.log(location.lng);
}

// stop watching

function logout() {
    clearInterval(activeWatch);
}
查看更多
登录 后发表回答