Possible Duplicate:
Reverse Geocode On Google Maps api v3
I want to find the address by using lat-lang value for that I’m using new google.maps.Geocoder().geocode()
following is the implementation of the method
function checkAddress(){
var address="";
var amstr = new google.maps.LatLng(37.556179,-122.288219);
address = getAddress(amstr);
alert(address);
}
function getAddress(latLng) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'latLng': latLng},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[0]) {
return results[0].formatted_address;
}
else {
return "No results";
}
}
else {
return "not working";
}
});
}
but after running above code I’m getting ‘undefined’ as result.
Please tell me where I’m doing it wrong.
Thanks
geocoder.geocode()is asynchronous. It only calls your callback some time after the rest of your code finishes.You need to return the value in a callback: