【问题标题】:Android Geolocation onError PhonegapAndroid Geolocation onError Phonegap
【发布时间】:2013-07-23 12:21:20
【问题描述】:

也许有人可以对此有所了解。

我正在使用 Phonegap 构建一个适用于 Android 的移动应用,该应用使用地理位置和 FourSquare 的 API。我想让应用程序离线运行,所以我在 getCurrentPosition 方法中使用了 onError 方法。神秘的是,这仅在某些时候有效。

我已经尝试了所有可能的可能性,包括使用设备的无线和/或 GPS 开/关进行测试,结果是一样的:有时会调用 onError 方法,而有时却不会……在线的时候就好了。

我已经包含了 javascriptcode 的基本部分,如果您发现任何基本错误或有任何建议,请告诉我。我也可以上传其余的......提前谢谢。

// Wait for Cordova to load 
document.addEventListener("deviceready", onDeviceReady, false);


// Cordova is ready 
function onDeviceReady() {
   navigator.geolocation.getCurrentPosition(onSuccess, onError);
}


// onSuccess Geolocation 
function onSuccess(position) {


$('#buttonNo1').click(checkLocation);


}

function checkLocation() {

    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

     var url2 = 'https://api.foursquare.com/v2/venues/search?ll='+latitude+','+ longitude + '&radius=10&oauth_token=FZGTD3IDTVJVJPFDARSSMZGVOTMK2ZOUPLHHYZKKN1JYJ11D&v=20130627&callback=?';

    $.get(url2, function(data) {

            venue0 =  data.response.venues[0].categories[0].name;
            venue1 =  data.response.venues[1].categories[0].name;
            venue2 =  data.response.venues[2].categories[0].name;
            populateHTML(venue0, venue1, venue2);

            }, "jsonp");

}


function onError(error) {

    populateHTMLManually();

}

【问题讨论】:

    标签: geolocation cordova onerror


    【解决方案1】:

    您没有理由需要互联网连接来更新您的位置。即使设备上的 GPS 和 Wifi 均已关闭,也应该可以使用小区三角测量来计算出大致位置。

    如果您希望 Android 设备使用 GPS 接收器(如果设备有),则在请求位置时必须使用 enableHighAccuracy 选项:

    navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy: true});
    

    关于离线时的错误,可能是因为默认超时值在收到位置更新之前到期(请参阅phonegap Geolocation API documentation。尝试同时增加超时和 maxiumumAge:

    navigator.geolocation.getCurrentPosition(onSuccess, onError, {
      enableHighAccuracy: true,
      timeout: 30000,
      maximumAge: 30000
    });
    

    虽然我不确定您的预期用户体验应该如何,但您可能需要考虑使用 watchPosition 而不是 getCurrentPosition。这会设置一个观察者,每次设备接收到位置更新时调用 onSuccess 函数,无论是来自 GPS 接收器、WiFi 还是小区三角测量。

    【讨论】:

      【解决方案2】:

      感谢 Dpa99c 和 Mohit 的解决方案,我决定朝这个方向发展:

      我决定在 onDeviceReady() 方法中包含一个检查互联网连接的函数。

      这是最终代码,它工作正常,但仍然需要尝试使用 Dpa99c 的想法来使用 watchPosition ...我已经使用 navigator.network.connection.type 来确定是否有 Internet 连接,看起来工作至今...

      // Wait for Cordova to load 
      document.addEventListener("deviceready", onDeviceReady, false);
      
      
      // Cordova is ready 
      function onDeviceReady() {
           checkConnection();
          navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy: true,
          timeout: 30000,  maximumAge: 30000});
      
      }
      
      
      // onSuccess Geolocation 
      function onSuccess(position) {
      
      
      $('#buttonNo1').click(checkLocation);
      
      
      }
      
      function checkLocation() {
      
          var latitude = position.coords.latitude;
          var longitude = position.coords.longitude;
      
           var url2 = 'https://api.foursquare.com/v2/venues/search?ll='+latitude+','+ longitude + '&radius=10&oauth_token=FZGTD3IDTVJVJPFDARSSMZGVOTMK2ZOUPLHHYZKKN1JYJ11D&v=20130627&callback=?';
      
          $.get(url2, function(data) {
      
                  venue0 =  data.response.venues[0].categories[0].name;
                  venue1 =  data.response.venues[1].categories[0].name;
                  venue2 =  data.response.venues[2].categories[0].name;
                  populateHTML(venue0, venue1, venue2);
      
                  }, "jsonp");
      
      }
      
      
      function onError(error) {
      
          populateHTMLManually();
      
      }
      function checkConnection() {
      
      networkState = navigator.network.connection.type
      if (networkState == Connection.NONE){
          $('#buttonNo1').click(function () {
          populateHTMLManually();
          });
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        相关资源
        最近更新 更多