ngCordova with watchPosition and clearWatch doesn't work

Hello,
I followed this documentation : http://ngcordova.com/docs/ with https://github.com/driftyco/ng-cordova

This following code doesn’t work :
var watchID = $cordovaGeolocation.watchPosition({ frequency: 1000 });

watch.promise.then(function() { // Not currently used }, function(err) { // An error occured. Show a message to the user }, function(position) { // Active updates of the position here // position.coords.latitude/longitude }); });

I think we have to replace this : watch.promise.then(function() {... and replaced by watch.then(function() {...

In addition, here, watchID is an object and not a simple number or integer

so, when we try this :
$cordovaGeolocation.clearWatch(watchID) => That’s doesn’t work !!!

For today, I had used the following code that works :
watchID = navigator.geolocation.watchPosition(function(position){... ... if (watchID != null) { $cordovaGeolocation.clearWatch(watchID); watchID = null; } $cordovaGeolocation.getCurrentPosition().then(function(position) {...

Can you upgrade this part of ng-cordova ?

Thank you

I had the same problem! Try “watchId” like this:

var watch = $cordovaGeolocation.watchPosition({ maximumAge: 10000, timeout: 30000 });    
watch.promise.then(...);

$cordovaGeolocation.clearWatch(watch.watchId);
1 Like

I got similar issue with $cordovaGeolocation.clearWatch and just wanted to update the thread … the returned object from $cordovaGeolocation.watchPosition has NO watchId property (but watchID).

1 Like

This works:
watch = $cordovaGeolocation.watchPosition(geo_options);
watch.then(null, onError, onLocation);
watch.clearWatch();

This doesn’t:
watch = $cordovaGeolocation.watchPosition(geo_options)
.then(null, onError, onLocation);
watch.clearWatch();

2 Likes

thanks man. that helped me :slight_smile:

1 Like

You saved me a lot of time! :slight_smile: