2014-06-29 66 views
1

我有一個問題,看起來非常直截了當,但我不明白我哪裏出錯了!PhoneGap - 地理位置錯誤

我使用的PhoneGap(2.9版本)來獲得地理定位數據。在初始化時調用以下函數(getGeolocationData)。問題是與位置服務的條件...

  1. 如果定位服務是申請後,啓動時,獲得地理定位數據 。 (OK)

  2. 如果位置服務在應用程序啓動時關閉,則會顯示錯誤 並再次調用地理定位功能。 (OK)

  3. 如果申請後,啓動時,獲得位置服務地理定位數據,但在使用過程中,如果我停用定位服務時,不會出現錯誤! (錯誤)。

注意:當位置服務關閉時,不再調用警報消息'geoGeo called'。

誰能幫我這個惱人的問題?

在此先感謝!

瑞安

function getGeolocationData() { 
    alert("getGeo called."); 
     var options = { maximumAge: 7000, timeout: 7000, enableHighAccuracy: true }; 
     watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 
     } 

      // onSuccess Geolocation 
      function onSuccess(position) { 
       lng = position.coords.longitude; 
       lat = position.coords.latitude; 
       acc = position.coords.accuracy;    
       alt = position.coords.altitude;     
       hdg = position.coords.heading;    
       spd = position.coords.speed;     


       geo_flag = 1; //obtained geolocation? 1 = Yes/0 = No 
       document.getElementById("geo-status").innerHTML = 'On'; 
       alert("success"); 

      } 

      // onError Callback receives a PositionError object 
      function onError(error) { 
       document.getElementById("tracking-status").innerHTML = 'Off'; 
       document.getElementById("geo-status").innerHTML = 'Off'; 
       geo_flag = 0; //obtained geolocation? 1 = Yes/0 = No 
       alert("failed"); 
       getGeolocationData(); 

      } 
+0

大家好,我剛剛更新到phoneGap 3.5.0,錯誤仍然存​​在!似乎不需要爲此找到解決方法.... – EvilGenius82

+0

我剛剛檢查了幾個網站,它似乎在phonegap的2.9版本中存在缺陷。我會盡快更新版本並在此發佈結果。謝謝瑞恩 –

回答

0

我假設你,因爲打開/關閉「位置服務」的iOS設備上進行測試呢?

好像當觀察者在應用程序中添加啓動時間,不能連續在每次更新的PhoneGap只檢查位置服務的可用性。由於您需要將您的應用置於後臺才能訪問設置以禁用位置設置,因此您可以嘗試使用Phonegap resume事件清除/重新添加觀察者,並查看是否導致Phonegap注意到位置服務的狀態已經改變。這樣的事情:

var watchID; 

function getGeolocationData() { 
    alert("getGeo called."); 
    var options = { maximumAge: 7000, timeout: 7000, enableHighAccuracy: true }; 
    if(watchID) navigator.geolocation.clearWatch(watchID); 
    watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 
} 

// onSuccess Geolocation 
function onSuccess(position) { 
    lng = position.coords.longitude; 
    lat = position.coords.latitude; 
    acc = position.coords.accuracy;    
    alt = position.coords.altitude;     
    hdg = position.coords.heading;    
    spd = position.coords.speed;     

    geo_flag = 1; //obtained geolocation? 1 = Yes/0 = No 
    document.getElementById("geo-status").innerHTML = 'On'; 
    alert("success"); 

} 

// onError Callback receives a PositionError object 
function onError(error) { 
    document.getElementById("tracking-status").innerHTML = 'Off'; 
    document.getElementById("geo-status").innerHTML = 'Off'; 
    geo_flag = 0; //obtained geolocation? 1 = Yes/0 = No 
    alert("failed"); 
    getGeolocationData(); 
} 

// Called on device being ready 
function onDeviceReady(){ 
    getGeolocationData(); // start geolocation tracking 
} 

// Called on resuming app from background 
function onResume(){ 
    alert("app resumed"); 
    getGeolocationData(); // reset geolocation tracking 
} 

document.addEventListener("deviceready", onDeviceReady, false); 
document.addEventListener("resume", onResume, false);