ngGeolocation icon indicating copy to clipboard operation
ngGeolocation copied to clipboard

Illegal invocation $apply when using watchPosition

Open BerendWeij opened this issue 8 years ago • 6 comments

I am using the google maps example. Angular 1.4.3 gives me several 'illegal invocation' errors.

TypeError: Illegal invocation at equals (angular.js:1039) at equals (angular.js:1039) at Scope.parent.$get.Scope.$digest (angular.js:15677) at Scope.parent.$get.Scope.$apply (angular.js:15951) at retVal.watchPosition.options.watchId.$window.navigator.geolocation.watchPosition.$rootScope.$apply.retVal.position.error (ngGeolocation.js:43)

Do you know how to solve this?

BerendWeij avatar Sep 16 '15 13:09 BerendWeij

Can you paste the code that you are using to call watchPosition?

ninjatronic avatar Sep 17 '15 07:09 ninjatronic

I'm seeing this too (not using angular-google-maps though). Note the example shown on the README seems wrong. Watching 'myPosition.position' is being watch by the scope, but that is not a scope variable.

I've tried to watch $geolocation.position in two ways:

$scope.$watch(function(){ return $geolocation.position; }, function() { ...})

$scope.$geolocation = $geolocation;
$scope.$watch('$geolocation.position', function() {});

cgat avatar Oct 30 '15 21:10 cgat

I've had issues with the example as well, until I realized that it's wrong. Reading the code, you can see that, when the position change, it emits an event with the tag '$geolocation.position.changed', so in your controller you have to use:

$scope.$on('$geolocation.position.changed', function(event, newPosition){
    console.log(newPosition); //This is the current position
})

tioma avatar Jan 04 '16 18:01 tioma

Thanks, it was exactly what I was looking for. Why not to update the readme?

bogomips avatar Jan 21 '16 20:01 bogomips

Feel free to update the readme and send a pr. I'm in Greece at the moment working with refugees and don't have a computer with me so can't do it myself. Thanks

On Thu, 21 Jan 2016 22:11 Pierluigi Petrelli [email protected] wrote:

Thanks, it was exactly what I was looking for. Why not to update the readme?

— Reply to this email directly or view it on GitHub https://github.com/ninjatronic/ngGeolocation/issues/21#issuecomment-173694483 .

ninjatronic avatar Jan 22 '16 12:01 ninjatronic

I had the same. After 2 hours of debugging I realized, that the example is wrong. Please update the example.

Disane87 avatar Sep 09 '16 20:09 Disane87