【问题标题】:GPS is not getting after disabling and enabling again in android with phonegapGPS在使用phonegap在android中禁用和再次启用后没有得到
【发布时间】:2015-07-22 16:50:44
【问题描述】:

我正在 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++;
}

【问题讨论】:

    标签: cordova phonegap-build


    【解决方案1】:

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

    我发现在 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();
    

    【讨论】:

      【解决方案2】:

      @nu6A, GPS 服务因电话和提供商而异。与 iPhone 不同的是,有许多制造商,而且手机制造商使用的芯片各不相同——即使是同一型号!

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

      祝你好运

      谷歌:android how does gps work

      【讨论】:

        猜你喜欢
        • 2019-01-04
        • 2011-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多