Organiccat
Organiccat

Reputation: 5651

Using geolocation inside angularjs

I've found that you have to use what I think is a workaround to get the geolocation data from the browser API. My question is why can't this be accessed the normal way when I can see the output data in the console log?

For example, in this case the lat and long are output, but the "mapoutput" is never updated:

function get_location() {
  navigator.geolocation.getCurrentPosition(show_map);
  console.log('nav: ', navigator);
}

function show_map(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;

  self.mapoutput = "http://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";
  console.log("latitude: ", latitude);
  console.log("longitude: ", longitude);
}

get_location();

I thought the show_map WAS the asynchronous callback, so why isn't self.mapoutput updated properly?

Upvotes: 1

Views: 158

Answers (1)

Dark Falcon
Dark Falcon

Reputation: 44201

Angular only updates data bindings when $digest is run. For callbacks set up through Angular functions, this is already done by Angular code. If you go outside of Angular you must do this yourself by wrapping the body of your callback in a call to $apply.

Upvotes: 1

Related Questions