2015-07-21 88 views
0

我正在与phonegap和cordova.js GPS相关的应用程序。我的应用在iOS中工作正常。在android中,每当我关闭GPS并重新启用它时,应用程序将无法识别GPS服务。我需要关闭并重新打开应用程序或使用location.reload(true)来引用应用程序。为什么它是这样的。我正在使用下面的代码段。全球定位系统没有得到禁用和再次启用Android PhonePhone

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++; 
} 

回答

1

@ nu6A, GPS服务从手机而异和供应商提供。与iPhone不同,有许多制造商,而手机制造商可能会改变他们使用哪种芯片 - 即使是在相同的型号下!

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

祝您好运

谷歌:android how does gps work

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();