Google MapsAPIGeocodeを使用して住所の緯度と経度を見つけようとする2つのJavascript関数があります。
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];
}
getLatLon()内のlocation[0]は#resultsdivに正しい数値を出力しますが、search()内のlocation[0]はundefinedを返します。なぜこれが起こるのか、何か考えはありますか?代わりにgetLatLon()からプレーン文字列( "Hello")を返してみましたが、これは問題なく機能します。