I am trying to get the address component from geo reverse coding and return the actual caller. But even the address is getting correctly the returned value is always undefined. This is how i am trying to get the address.
var geocoder;
var addresss = codeLatLng(23.750875259244058, 56822900823215);
alert(address);
//Getting the address through reverse geo coding
var address;
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
alert(results[0].formatted_address)
//find country name
for (var i = 0; i < results[0].address_components.length; i++) {
for (var b = 0; b < results[0].address_components[i].types.length; b++) {
//there are different types that might hold a city
admin_area_lvl_1 usually does in come cases looking
for sublocality type will
be more appropriate
if (results[0].address_components[i].types[b] ==
"administrative_area_level_1") {
//this is the object you are looking for
city = results[0].address_components[i];
break;
}
}
}
//city data
alert(city.short_name + " " + city.long_name)
address = city.short_name;
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
return address;
}