2015-07-21 30 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();