平台:iOS6/OSx Lion。
我正在尝试弄清楚 Phonegap/Cordova 与 navigator.geolocation.watchPosition
的工作方式。
文档说选项“maximumAge
”是要求系统检索位置的选项。
因此,使用这些选项:
{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }
我预计职位请求将每 3 秒触发一次?
无论 maximumAge
我把成功每 1 秒触发一次......
谁能解释一下吗?
谢谢再见
罗布
最佳答案
我目前正在通过将 getCurrentPosition
与 setInterval
结合使用来解决此问题。我不确定会产生什么后果,但这似乎给了我最大的控制权,并且似乎是跨平台最一致的方法。
// 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);
}
关于ios - Phonegap、Cordova 观察位置每 1 秒成功发射一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13254420/