How do I return the latlon variable for codeAddress function. return latlon doesn’t work, probably because of scope but I am unsure how to make it work.
function codeAddress(addr) {
if (geocoder) {
geocoder.geocode({ 'address': addr}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latlon = results[0].geometry.location.c+","+results[0].geometry.location.b;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
You cannot return the result of
geocoder.geocodefromcodeAddresssincegeocoder.geocodewill return its result to the callback/closure you provide. You have to proceed using a callback given as an argument to your functioncodeAddress.Returning anything from your callback given to
geocoder.geocodeback togeocoder.geocodewill not make any sense in your application. You have to call some function in your application from the callback you provide togeocoder.geocode.This is explained in Geocoding Requests section of the API.