Geolocation Watch Position throwing error

I’ve got the $cordovaGeolocation plugin installed and injected into my controller. I know the plugin is working because it is successfully getting my current position. However, when it tries to run the watch method I’m getting the error:

Error w/ watchPosition: [object Object]

Here is code below. I hope it is something rookie I’m doing.

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

    var watch = $cordovaGeolocation.watchPosition(watchOptions);

    watch.then(function(position) {
      var lat         = position.coords.latitude,
      long            = position.coords.longitude,
      currentLocation = new google.maps.LatLng(lat, long);
      currentPosMarker.setPosition(currentLocation);
    
      console.log('assigning your new position');
    
    }, function(error) {
        console.log('Error w/ watchPosition: ' + error);
    });
1 Like

what is the error? Change the code to something to dump the error?

console.log('Error w/ watchPosition: ' +JSON.stringify(error));

@aaronksaunders Never even thought of doing that, mad idea. The error is:

Error w/ watchPosition: {"code":2,"message":""}

Whats also interesting is when I change enableHighAccuracy to true

enableHighAccuracy: true // may cause errors if true

It gives me this error instead

{"code":3,"message":"Position retrieval timed out."}
1 Like

what device, is this on IOS or Android?

iOS. Running 8.1 on an iPhone 5.

Hi,

For enableHighAccuracy: true, i think that the GPS it’s not in fonction or you do not allow autorisation(see in device parameters) or not fixe satellite before TIME_OUT.

An possibity is: you don’t have data connexion on your device.
For me your code is Ok, I use watch in my code and I work find, but it’s an Android Device.

PERMISSION_DENIED (error 1): The location acquisition process failed because the document does not have permission to use the Geolocation API.
POSITION_UNAVAILABLE (error 2) The position of the device could not be determined. For instance, one or more of the location providers used in the location acquisition process reported an internal error that caused the process to fail entirely.
TIMEOUT (error 3): The length of time specified by the timeout property has elapsed before the implementation could successfully acquire a new Position object.

@jimibi How do you implement the watch in your code?

So I’ve got error 2 at the moment. Interesting.

When I’ve got:

enableHighAccuracy: true // may cause errors if true

And the timeout to 30000

frequency : 30000,
timeout : 30000,

I still get the TIMEOUT (error 3): The length of time specified by the timeout property has elapsed before the implementation could successfully acquire a new Position object.

Also note my location services are on, and my app has permission to use it.

An exemple of my code.

var watchId = {},
watchOptions = {
frequency: 15 * 60 * 1000,
timeout : 1 * 60 * 1000,
enableHighAccuracy: false
};

var watchId = $cordovaGeolocation.watchPosition(watchOptions);

watchId.then(

null,
function(err) {
     console.log('Error: ' + err);
},
function(position) {
  var latitude    = position.coords.latitude,
        longitude = position.coords.longitude;

  localStorageService.add('keyGPS', watchId);

});

Keep frequency > timeout.

1 Like

Cool! I’ll have to give that a try then.

What is the benefit of doing the timeout and frequency like this?

frequency: 15 * 60 * 1000,
timeout : 1 * 60 * 1000,

To immediately see the time in minutes, no benefit for programmation.

I assume you are calling getPosition() before you set watchPosition() ??

I had some issues in my app with watchPosition with the view caching. I was setting watchPosition() on $ionicView.afterEnter, but it turns out with view caching, the previous watch position was already in place, so the subsequent one(s) would time out.

So I ended up changing the code to:

if (!$scope.watchPositionID) {
  $scope.watchPositionID = navigator.geolocation.watchPosition(watchPositionSuccessCallback, watchPositionErrorCallback, { timeout: 30000 });
}

Then on $scope.$on('$destroy) I would do:

navigator.geolocation.clearWatch($scope.watchPositionID);
$scope.watchPositionId = null;

And it seems to work a lot better if the view was cached or destroyed if the app started running out of resources.

I also found that calling getPosition() before setting watchPosition tend to work better too - mainly because it forces the operating system message asking the user if they mind the app using geolocation.

@CyberFerret Thanks for the gotchas!

How come you start a new watch every time the view becomes active?

Also, when developing with your implementation, did the watch still work when you were on a different view that was hooked up to a different ctrl. Or does the watch only work when you are on that view, and then when the user moves to a different view the watch is cancelled?

Cheers.

A bit late to the discussion, but I am having the same issues on iOS, when I use enableHighAccuracy = true, I get the error code 3

Tested on both iOS 7 and 8. Any updates about this issue? Works fine on Android

Code 3 from memory is a timeout, where it cannot get your location for whatever reason and so it gives up trying to get it and times out. I know this is pretty basic, but have you got location services enabled. And if so, for your app?

Is it a must to call get currentposition b4 the watch position ? I tried all above tips and keep getting time out error on the watchposition. Am testing the code on real device ios 8.1 i think.

I cant see my application in the setting to enable the privacy option for the qeolocation.

There’s a really simple workaround for this problem.

First wrap your geolocation code into device ready event, like this:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

}	

Give it high enough timeout, from my experience 20000 is more than enough.

Read more about it here.

For me the timing out problem was that the Cordova Geolocation plugin will time out if enableHighAccuracy is true and the user doesn’t move five meters within that timeout period. See: http://stackoverflow.com/questions/30989428/apache-cordova-geolocation-watchposition-times-out-on-ios-when-standing-still

This is a BIG assumption for the geolocation plugin to make. Shouldn’t the developer be able to determine the distanceFilter?!

Please vote on this issue: https://issues.apache.org/jira/browse/CB-7875

1 Like

I was having the same error with Ionic 1, Cordova 6.5.0, cordova-plugin-geolocation 2.4.2, on iPhone5s with iOS 10.2

I got rid of it by omitting the “timeout” and “maximumAge” options (actually I think that removing the timeout was the clue).

so my watchOptions looks like:
var watchOptions = {
enableHighAccuracy: true
};

1 Like