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

时间:2016-05-06 14:03:28

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

我正在使用cordova和ionic在app ios / android上工作。 cordova插件geolocation版本为2.2.0。

它在Android上工作得很好。 但是在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);

}); 

3 个答案:

答案 0 :(得分: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;
  };

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

在某些情况下它并不适合我 - 而且错误代码并不一致 - 所以南极洲有一个回落...

答案 1 :(得分:0)

我通过这样做解决了我的问题: 当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 :(得分: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() times out on iOS when standing still