首页 文章

android中的apache cordova中的地理位置(getcurrentlocation)无法正常工作

提问于
浏览
0

我正在开发使用cordova for android的混合移动应用程序 . 我希望从设备获取当前位置,因为我正在使用cordova中的地理定位插件 . 但是在调用之后

navigator.geolocation.getCurrentPosition(onSuccess,onError);

它始终是onError回调并显示超时错误 . 如果我给出超时选项

如果我没有给出timout选项..超时错误没有显示,输出也没有来,请为此提供一些解决方案 .

2 回答

  • 0

    首先,应用程序启动时双重检查GPS设备是否开启, - )

    其次,尝试在getCurrentPosition调用中添加选项enableHighAccuracy false,或者使用cache,option maximumAge进行测试 . 如果你没有长时间超时的答案,可能GPS设备很慢 . 获取GPS信息最多可能需要25秒 . 请耐心等待 - )

    navigator.geolocation.getCurrentPosition(
                function(positionGPS){console.log(positionGPS);},
                function(){console.log('no gps');},
                {enableHighAccuracy:false,maximumAge:Infinity, timeout:60000}
               );
    
  • 0

    我尝试了多种策略,直到找到结果,如果不是我每分钟都拉一次 . 或多或少是这样的:

    function calculatePosition(timeout, maxAge, accuracy, step){
      navigator.geolocation.getCurrentPosition(
          function(position){
            console.log("success");
          },
          function(error){
            nextStep(step + 1);
          }, 
          {enableHighAccuracy: accuracy,
          timeout: timeout,
          maximumAge: maxAge}
        );
    }
    
    function nextStep(step){
      switch(step){
        case 0:
          calculatePosition(1000, 604800000, false, step);
          break;
        case 1:
          calculatePosition(1000, 604800000, true, step);
          break;
        case 2:
          calculatePosition(5000, 4000, false, step);
          break;
        case 3:
          calculatePosition(5000, 4000, true, step);
          break;
        case 5:
          calculatePosition(50000, 60000, true, step);
          break;
        case 6:
          calculatePosition(50000, 60000, false, step - 2);
          break;      
      }
    }
    
    nextStep(0);
    

相关问题