-2

My function is like this.

function getLatLong(address){
    var latitude="";
    var geocoder =  new google.maps.Geocoder();

    geocoder.geocode( { 'address': address}, function(results, status) {
        latitude = results[0].geometry.location.lat();
        alert(latitude);
});
   return latitude;
}


  alert(getLatLong(co));

when i call this function I get empty result but the alert in the function returns the latitude.what the problem?

duncan
  • 31,401
  • 13
  • 78
  • 99
  • the geocode request is asynchronous, so your return statement can execute before the geocode function completes. See https://developers.google.com/maps/documentation/javascript/geocoding – duncan May 20 '14 at 10:27

1 Answers1

0

I think the ops answer in this question might help :

Google maps geocode API V3 not returning result in javascript function

As you can see in the solution, the op uses a callback from geocoder.geocode.

Community
  • 1
  • 1
aarcarr
  • 186
  • 1
  • 14