【问题标题】:Cordova ionic geolocation fails : Position retrieval timed out error code 3 on iOSCordova 离子地理定位失败:iOS 上的位置检索超时错误代码 3
【发布时间】:2016-09-01 15:32:20
【问题描述】:

我正在使用 cordova 和 ionic 开发应用程序 ios/android。 cordova 插件地理定位是 2.2.0 版本。

在安卓上运行良好。 但是在 ios 上,从观察者那里收到新位置 4 次后,我有以下错误:
PositionError {code: 3, message: "Position retrieval timed out.", PERMISSION_DENIED: 1, POSITION_UNAVAILABLE: 2, TIMEOUT: 3}

有人有办法吗?

这是我的代码的一部分:

var posOptions = {
    timeout           : 10000,
    enableHighAccuracy: false
};

var watchOptions = {
  timeout : 10000,
  enableHighAccuracy: false // may cause errors if true
};  

/**
 * Sets initial user position.
 */
$ionicPlatform.ready(function () {
  console.log('ready');
    $cordovaGeolocation
        .getCurrentPosition(posOptions)
        .then(function (position) {
            setLocationData(position);
        }, function (err) {
            // error
        });

    /**
     * Watches for user position.
     */
    $timeout(function() {
      console.log(watchOptions);
      var watch = $cordovaGeolocation.watchPosition(watchOptions);
      watch.then(
        null,
        function (err) {
          // error
          console.log(watchOptions);
          console.log(err);
          alert(err);
        },
        function (position) {
          console.log(watchOptions);
          console.log('refresh')
          alert('refresh');
          setLocationData(position);
        });
    }, 10000);

}); 

【问题讨论】:

    标签: ios cordova ionic-framework geolocation cordova-plugins


    【解决方案1】:

    我通过这样做解决了我的问题: 当 Watcher 有错误时。停止它并重新启动。

    这里是我的代码:

     /**
         * Sets initial user position.
         */
        $ionicPlatform.ready(function () {
          console.log('ready');
            $cordovaGeolocation
                .getCurrentPosition(posOptions)
                .then(function (position) {
                    setLocationData(position);
                }, function (err) {
                    // error
                });
    
            /**
             * Watches for user position.
             */
            $timeout(function() {
              console.log(watchOptions);
              watchLocation();
            }, 10000);
    
        });
    
        function watchLocation(){
            var watch = $cordovaGeolocation.watchPosition(watchOptions);
            watch.then(
              null,
              function (err) {
    
                watch.clearWatch();
                watchLocation();
              },
              function (position) {
    
                setLocationData(position);
              });
        }
    

    【讨论】:

      【解决方案2】:

      很抱歉,我无法为您提供更多帮助,但这是我过去使用的代码...

      (这是一个使用 Phonegap 构建的 Cordova 应用程序)

        var getLocation = function() {
          var deferred = Q.defer();
            var options = {
              enableHighAccuracy: true,
              timeout: 5000,
              maximumAge: 0
            };
            var onSuccess = function(position) {    
              var lat = position.coords.latitude;
              var lng = position.coords.longitude;
              deferred.resolve({lat : lat, lng : lng});
            };
      
            var onError = function() {
              var lat = -77.847635; 
              var lng = 166.760616;
              deferred.resolve({lat : lat, lng : lng});
            };
      
            if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
            } else {
              onError();
            }
          return deferred.promise;
        };
      

      有很多事情可能会出错。首先,应用程序是否要求用户允许使用该位置?

      在某些情况下它对我不起作用 - 并且错误代码不一致 - 所以有一个后备到南极洲......

      【讨论】:

      • 是的应用程序请求权限,应用程序给我 4 次正确的位置,在我收到错误“位置检索超时”后,这很奇怪,因为它在 android 上运行得很好
      【解决方案3】:

      我通过修改 iOS Cordova 插件 (CDVLocation.m) 解决了如下问题:

      if (enableHighAccuracy) {
          __highAccuracyEnabled = YES;
          // Set distance filter to 5 for a high accuracy. Setting it to "kCLDistanceFilterNone" could provide a
          // higher accuracy, but it's also just spamming the callback with useless reports which drain the battery.
          //self.locationManager.distanceFilter = 5; //OLD CODE
          self.locationManager.distanceFilter = kCLDistanceFilterNone; //NEW CODE
          // Set desired accuracy to Best.
          self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      } else {
          __highAccuracyEnabled = NO;
          //self.locationManager.distanceFilter = 10; //OLD CODE
          self.locationManager.distanceFilter = kCLDistanceFilterNone; //NEW CODE
          self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
      }
      

      来源:Apache Cordova geolocation.watchPosition() times out on iOS when standing still

      【讨论】:

        猜你喜欢
        • 2020-08-29
        • 1970-01-01
        • 2016-03-26
        • 1970-01-01
        • 1970-01-01
        • 2019-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多