0

I'm trying to retrieve an array ([latitude,longitude]) from Google Maps' API by returning the said array and putting it in variable 'latlong'. However, nothing seems to be returned by the function. What am I doing wrong here? I realize the function is called in a and of embedded way, but being new to Js, I find the whole thing is confusing.

This code:

// Withing another function:

var latlong = geocoder.geocode( { 'address': 'London, UK'}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
      var latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
  }
  return [latitude,longitude];
});					
console.log(latlong);

returns 'undefined'

2
  • 1
    geocoder.geocode is an async operation, there are several ways to get the correct value: callback, promise etc Commented Jan 31, 2018 at 17:02
  • 1
    @Rodolphe might be useful stackoverflow.com/questions/20028808/… Commented Jan 31, 2018 at 17:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.