【问题标题】:Cordova watchPosition does not restart the GPS request on phoneCordova watchPosition 不会重新启动手机上的 GPS 请求
【发布时间】:2015-07-23 18:44:59
【问题描述】:

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

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

您有什么想法,如何通过 GPS 请求重新启动 watchPosition?

谢谢

【问题讨论】:

    标签: android cordova gps geolocation


    【解决方案1】:

    我发现清除现有的观察者然后重新添加一个新的观察者可以解决 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();
    

    【讨论】:

    • 谢谢,伙计!你让我开心:)
    • 不用担心,很高兴我能帮上忙。为了他人的利益,请您将此标记为已接受的答案
    最近更新 更多