Please note, the arguments 'origin' and 'destination' are both objects of type google.maps.LatLng.
I am trying to reverse-geocode these LatLng objects in order to obtain and return formatted addresses.
The function simply assigns 'undefined' to 'geocodedOrigin' and 'geocodedDestination'. I would like to know why the geocoder is not working? Thanks.
function geocodeLocations(origin, destination)
{
var geocoder = new google.maps.Geocoder();
var geocodedOrigin;
var geocodedDestination;
var geocodedLocations;
geocoder.geocode({'location': origin}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
geocodedOrigin = results[1].formatted_address;
}
else {
geocodedOrigin = 'No results found';
}
}
else {
geocodedOrigin = 'Geocoder failed due to: ' + status;
}
});
geocoder.geocode({'location': destination}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
geocodedDestination = results[1].formatted_address;
}
else {
geocodedDestination = 'No results found';
}
}
else {
geocodedDestination = 'Geocoder failed due to: ' + status;
}
});
geocodedLocations = [geocodedOrigin, geocodedDestination];
return geocodedLocations;
}