Phonegap,Cordova watchposition火力每1秒成功一次

时间:2012-11-06 15:40:19

标签: ios cordova geolocation

平台:iOS6 / OSx Lion。

我试图解决Phonegap / Cordova与navigator.geolocation.watchPosition合作的方式。

文档说选项“maximumAge”是要求系统检索位置的选项。

使用这些选项:

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

我认为位置请求会每3秒触发一次?

无论我maximumAge我的成功是每1秒发射一次......

任何人都可以解释一下吗?

谢谢再见 罗布

1 个答案:

答案 0 :(得分:5)

我目前正在使用getCurrentPositionsetInterval来解决此问题。我不确定后果可能是什么,但这似乎给了我最多的控制权,似乎是跨平台最一致的方法。

// 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);
}