ある関数から別の関数に2つの座標を渡す際に問題が発生しています。私は実際にJavaScriptを知らないが、いくらか正しいと思われる。私の誤りがどこにあるのか教えてください。ある関数から別の関数に変数を渡すJavaScript
<script type="text/javascript">
function initialize() {
var address = "Chicago IL";
locs= getLatLong(address);
alert(locs.lat()); //does not work
var myOptions = {
zoom: 4,
center: new google.maps.LatLng(41, -87),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
}
function getLatLong(address){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
locations[0] = results[0].geometry.location.lat();
locations[1] = results[0].geometry.location.lng();
//alert(locations[0]); //test is good- works! Need to pass it back to function
//alert(locations[1]); //test is good- works! Need to pass it back to function
locs =results[0].geometry.location;
return locs;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
私は、基本的な(まだ難しい)理解するのを助けるかもしれない同様の質問をしました:http://stackoverflow.com/questions/8436358/understanding-local-scope – ajax333221