【问题标题】:Phonegap, Cordova watchposition fire success every 1 secondPhonegap,Cordova watchposition 每 1 秒触发一次成功
【发布时间】:2012-10-26 14:32:30
【问题描述】:

平台:iOS6/OSx Lion。

我试图弄清楚 Phonegap/Cordova 与 navigator.geolocation.watchPosition 的工作方式。

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

所以有了这些选项:

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

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

而且不管maximumAge我把成功每1秒触发一次...

谁能解释一下?

谢谢再见

【问题讨论】:

  • 这里有同样的问题。科尔多瓦 2.2 和 iOS 5.1 / iOS6 。 iOS 每秒触发一次,Android 似乎每 30 次触发一次,但不太可靠。
  • 嗨 nickhar,所以它在 Android 中也不起作用?真的很奇怪,我认为我使用了错误的参数,在以前版本的 Cordova/Phonegapp 中有一个“频率”参数。谢谢Ciao Rob

标签: ios cordova geolocation


【解决方案1】:

我目前正在通过使用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);
}

【讨论】:

  • 嗨烫伤,感谢您的回复。实际上,函数 watchPosition 似乎只是对 getCurrentPosition 的调用(带有计时器)(我查看了 cordova.js 内部)。顺便说一句,您的代码非常有帮助!当您使用“clearInterval(activeWatch)”停止计时器时,它也会停止“watchPosition”吗?我的意思是如果多次调用“setupWatch”,可能会有很多带有“watchPosition”的计时器?非常感谢 Ciao Rob
  • Rob——我的代码中没有使用 watchPosition。我知道科尔多瓦表面之下的某个地方可能正在做类似的事情,但这对我来说比他们的watchPosition 电话要好得多。是的,clearInterval 调用会停止 watchLocation 并清除该计时器,以便后续对 setupWatch 的调用可以创建一个新的计时器。
  • 嗨,Scald,谢谢,我会试试你的代码,我很确定这是 cordova.js 与 iOS 有一些问题。非常感谢!巧抢
  • 嗨烫伤,我试过你的代码,它可以工作。问题是当应用程序处于后台时 getCurrentPosition 不起作用。所以我不能用于跟踪 gps 位置。我查看了 Phonegap 的 Android 源代码,这里似乎在 iOS 源代码中实现了“maximumAge”参数(在 GeoBroker.java 第 67 行),没有提到“maximumAge”!我在 Phonegap Google 组中创建了一个线程,也许这需要作为一个错误发布到 Phonegapp Github。非常感谢 Ciao Rob
  • 嗨烫伤,已接受答案。 Phonegap 的 Shazron 回复了我,其中包含用于管理 maximumAge 选项的代码部分(请参阅:goo.gl/SDTj7)。顺便说一句,在我看来,这并不意味着设置检索当前用户位置的时间,而是在新的位置上获取最后一个缓存的位置(似乎是徒劳的?)所以它的 gps 设备仍然每秒运行一次......必须尝试一些其他方式...也许用您的代码在 watchPosition 上触发计时器?无论如何,谢谢 ciao Rob
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多