gpt4 book ai didi

android - Cordova watchPosition 不会重新启动手机上的 GPS 请求

转载 作者:行者123 更新时间:2023-11-30 02:00:15 25 4
gpt4 key购买 nike

我正在使用 Cordova 地理定位插件。

当手机准备就绪时,watchPosition 会开始。如果在启动应用程序之前激活了 GPS,则一切正常。但是如果我之后启动 GPS,watchPosition 不会在我的手机上启动 GPS 请求,所以 watchPosition 只会频繁显示错误消息。

你知道如何使用 GPS 请求重启 watchPosition 吗?

谢谢

最佳答案

我发现清除现有观察者然后重新添加新观察者可以解决 Android 上的这个问题;像这样:

var MAX_POSITION_ERRORS_BEFORE_RESET = 3,
positionWatchId = null,
watchpositionErrorCount = 0;

function addWatch(){
positionWatchId = navigator.geolocation.watchPosition(onWatchPositionSuccess, onWatchPositionError, options);
}

function clearWatch(){
navigator.geolocation.clearWatch(positionWatchId);
}


function onWatchPositionError(err) {
watchpositionErrorCount++;
if (watchpositionErrorCount >= MAX_POSITION_ERRORS_BEFORE_RESET) {
clearWatch();
addWatch();
watchpositionErrorCount = 0;
}

}
addWatch();

关于android - Cordova watchPosition 不会重新启动手机上的 GPS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31595650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com