【问题标题】:PhoneGap - Geolocation ErrorPhoneGap - 地理位置错误
【发布时间】:2014-06-29 11:17:08
【问题描述】:

我有一个看起来非常直接的问题,但我不明白我哪里出错了!

我正在使用 PhoneGap(2.9 版本)来获取地理位置数据。以下函数 (getGeolocationData) 在初始化时被调用。问题在于位置服务条件...

  1. 如果定位服务在应用程序启动时开启,地理定位 得到数据。 (好的)

  2. 如果位置服务在应用程序启动时关闭,则会出现错误 显示并再次调用地理定位函数。 (好的)

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

            }

【问题讨论】:

  • 我刚刚检查了几个站点,似乎 phonegap 的 2.9 版本存在缺陷。我会尽快更新版本并在此处发布结果。谢谢瑞恩
  • 大家好,我刚刚更新到phoneGap 3.5.0,错误仍然存​​在!似乎不需要为此找到解决方法....

标签: cordova geolocation


【解决方案1】:

由于打开/关闭“定位服务”,我假设您正在 iOS 设备上进行测试?

似乎 Phonegap 仅在应用启动时添加观察者时检查位置服务的可用性,而不是在每次更新时持续检查。由于您需要将应用程序置于后台以访问设置以禁用位置设置,因此您可以尝试使用 Phonegap 恢复事件来清除/重新添加观察者,并查看这是否导致 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);

【讨论】:

    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多