0

The console.log is giving an undefined value of the coord. If we console.log coord inside the if function, it gives the value, but it seems like the values cannot be coming out from the geocode function.

Does anyone know what the scope of geocode is and how can I change to get the values out from the geocode function?

THANKS

    var coord;

    function change_coord(location) {
    var geocoder = new google.maps.Geocoder();
      geocoder.geocode({'address': location}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              //real_lat and real_lng are int values 
              var real_lat = results[0].geometry.location.lat();
              var real_lng = results[0].geometry.location.lng();
              coord = {lat: real_lat, lng: real_lng};
          }
      });
  console.log(coord); //undefined
  }
  • 2
    I have not used GeoCoder but looks like you need to put console.log(coord) inside the if block because geocoder.geocode looks like some kind of async call to back end and it takes time to give result and before that returns you are printing coord – jsmtslch Mar 08 '17 at 21:52

1 Answers1

2

Try with console.log inside the geocoder callback function. In your code console.log is executed before you have receive the geocode response.

Butch
  • 56
  • 4