I have two Javascript functions that try to find the latitude and longitude of an address using Google Maps API Geocode:
function getLatLon(address) {
var location = -1;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat();
var lon = results[0].geometry.location.lng();
location = new Array(lat, lon);
//document.getElementById('results').innerHTML = location[0];
} else {
alert("Geocode was not successful.");
}
});
return location;
}
function search() {
var address = document.getElementById('address').value;
var location = getLatLon(address);
document.getElementById('results').innerHTML = location[0];
}
While the location[0] inside getLatLon() prints out the correct number in the #results div, the location[0] inside search() returns undefined. Do you have any ideas why this might happen? I've tried returning a plain string ("Hello") from getLatLon() instead and that works just fine.
geocoder.geocodeasynchronous? If yes,getLatLonshould always return -1. AlwaysgetLatLonreturningundefined?locationis-1, so it should be returning-1as well.location[0]is undefined notgetLatLon(). Poor question wording IMO.