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'

Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
Lucien S.
  • 5,123
  • 10
  • 52
  • 88
  • 1
    `geocoder.geocode` is an async operation, there are several ways to get the correct value: callback, promise etc – uladzimir Jan 31 '18 at 17:02
  • 1
    @Rodolphe might be useful https://stackoverflow.com/questions/20028808/using-a-callback-function-with-google-geocode – uladzimir Jan 31 '18 at 17:03

0 Answers0