2

I'm using following code snippet to obtain the current geolocation:

angular.module('HomelandApp').controller('PlacesController', function($scope,$http, $localStorage, uiGmapGoogleMapApi) {

  uiGmapGoogleMapApi.then(function(maps) {

        navigator.geolocation.getCurrentPosition(function (pos) {
            $scope.map = { center: { latitude: pos.coords.latitude, longitude: pos.coords.longitude }, zoom: 17 };
        });
  });
});

The problem I'm facing is that I somehow have to invoke the controller twice in order to get the map rendered. This is happening when using navigator so it's not a pure Angular UI problem.

I guess they're digest cycle Angular against native JS details. How should I fix this issue?

1 Answer 1

2

navigator.geolocation is outside of angular core so any scope changes made within it need to notify angular to run a digest to update the view. There are several ways to do that.

Try:

 navigator.geolocation.getCurrentPosition(function (pos) {
            $scope.map = { .... };
            $scope.$apply();
});
Sign up to request clarification or add additional context in comments.

4 Comments

Could you update your question telling longer about those several ways to do it?
wrapping in $timeout() is another ... will call $apply internally but will be pushed to end of any existing digest cycle (if thre is one active)
More useful if you extend your answer ;)
I assure you there are lots of similar answers with a lot more detail around

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.