Jeffrey
Jeffrey

Reputation: 472

Cordova ionic geolocation fails : Position retrieval timed out error code 3 on iOS

I'm working on an app ios/android using cordova and ionic. cordova plugin geolocation is in version 2.2.0.

it's working good on android. but on ios, after receiving new position from the watcher 4 times, i have the following error :
PositionError {code: 3, message: "Position retrieval timed out.", PERMISSION_DENIED: 1, POSITION_UNAVAILABLE: 2, TIMEOUT: 3}

somebody have a solution ?

here a part of my code :

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);

}); 

Upvotes: 1

Views: 3934

Answers (4)

mike nelson
mike nelson

Reputation: 22136

In my case I got this same error but actually there was a warning in NSLog - the error should have been "No NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key is defined in the Info.plist file". Solved by adding NSLocationWhenInUseUsageDescription to info properties in xcode.

Upvotes: 0

Valerio Gentile
Valerio Gentile

Reputation: 1100

I have solved by modifying the iOS Cordova Plugin (CDVLocation.m) as follow:

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;
}

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

Upvotes: 0

Jeffrey
Jeffrey

Reputation: 472

I solved my issue by doing this : when Watcher have errors. stop it and restart.

here my code :

 /**
     * 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);
          });
    }

Upvotes: 1

Mars Robertson
Mars Robertson

Reputation: 13213

I'm really sorry that I cannot help you more but this is the code I was using in the past...

(it was a Cordova application using Phonegap build)

  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;
  };

There are plenty things that can possibly go wrong. First of all does the app ask user for permission to use the location?

In some instances it wasn't working for me - and the error codes weren't consistent - so there is a fallback to Antarctica...

Upvotes: 0

Related Questions