2014-06-29 67 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);