Cordova 离子地理定位失败:iOS 上的位置检索超时错误代码 3

问题描述 投票:0回答:4

我正在使用 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
4个回答
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);
          });
    }

0
投票

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

(这是一个使用 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;
  };

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

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


0
投票

我通过修改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() 在 iOS 上静止时超时


0
投票

在我的例子中,我遇到了同样的错误,但实际上 NSLog 中有一个警告 - 错误应该是“Info.plist 文件中没有定义 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 键”。通过将 NSLocationWhenInUseUsageDescription 添加到 xcode 中的信息属性来解决。

© www.soinside.com 2019 - 2024. All rights reserved.