0

I'm working with google map api V3 and want to get location for some address by using next function:

function findAddress(address) {

    var geocoder = new google.maps.Geocoder();
    var addrLocation = "";

    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        addrLocation = "Location is: " + results[0].geometry.location;
    } else {
        addrLocation = "Not found"; }   
    });

    return addrLocation;
}

Then I'm trying to call findAddress function:

function SomeFunction(){
  alert(findAddress("New York"));
}

To my logic, that alert should return 'addrLocation' value, but instead it returns blank message. What am I doing wrong?

mbigun
  • 1,304
  • 4
  • 19
  • 46
  • 1
    possible duplicate of [How do I return a variable from Google Maps JavaScript geocoder callback?](http://stackoverflow.com/questions/2993563/how-do-i-return-a-variable-from-google-maps-javascript-geocoder-callback) – epascarello Jan 29 '13 at 14:16
  • Thanks a lot! Also I found the same problem [retrieving lat/long of location using google.maps.geocoder](http://stackoverflow.com/questions/8807141/retrieving-lat-long-of-location-using-google-maps-geocoder). It's impossible to return value from an asynchronous call, but I can store this values in global variables for example. Thanks to all. – mbigun Jan 29 '13 at 16:37

1 Answers1

3

You can not return a value from an asynchronous call, going to find a dupe since this is asked hourly.

epascarello
  • 204,599
  • 20
  • 195
  • 236