在 android 中使用 phonegap 禁用并再次启用后 GPS 无法获取

GPS is not getting after disabling and enabling again in android with phonegap

我正在 phonegap 和 cordova.js 中使用与 GPS 相关的应用程序。我的应用程序在 iOS 中运行良好。在 android 中,每当我关闭 GPS 并再次启用它时,应用程序将无法识别 GPS 服务。我需要关闭并重新打开该应用程序或使用 location.reload(true) 引用该应用程序。为什么会这样。我正在使用以下代码段。

refrshWatchPoss = setInterval(function () {
            navigator.geolocation.getCurrentPosition(onWatchPositionSuccess, onWatchPositionError, options);
        }, 5000);

function onWatchPositionError(err) {
    window.plugins.toast.showLongBottom('App is waiting for location service! ', function (a) {
    }, function (b) {
    })
    if (watchpositionErrorCount >= 9) {        
        messageAlert('Location service is unavailable. If it is unavailable after restarting then go to settings and reset your location services');        
        $.mobile.loading('show');
        navigator.splashscreen.show();
        setTimeout(location.reload(true), 2000);
    }
    watchpositionErrorCount++;
}

@nu6A, GPS 服务从 phone 到 phone 和供应商不等。与 iPhone 不同的是,有许多制造商,phone 的制造商使用的芯片各不相同 - 即使是同一型号!

如果您遇到此问题,则需要解决它。您还应该尽可能多地查找文档。

祝你好运

Google: android how does gps work

我建议在间隔上使用 watchPosition 而不是 getCurrentLocation,因为每次操作系统从 GPS 硬件检索位置更新时 watchPosition 都会调用成功函数,而不是在特定的时间间隔。

我发现在 Android 上,通过清除并重新添加位置观察器,这解决了位置服务关闭然后再次打开时的问题。所以,使用你上面的代码,是这样的:

var positionWatchId;

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

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


function onWatchPositionError(err) {
    window.plugins.toast.showLongBottom('App is waiting for location service! ', function (a) {
    }, function (b) {
    })
    if (watchpositionErrorCount >= 9) {        
        clearWatch();
        addWatch();
    }
    watchpositionErrorCount++;
}

addWatch();