【发布时间】:2012-01-17 06:28:09
【问题描述】:
我在将两个坐标从一个函数传递到另一个函数时遇到问题。我真的不知道 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>
【问题讨论】:
-
我问了一个类似的问题,可能会帮助你理解一些基本的(但一开始很难)理解:stackoverflow.com/questions/8436358/understanding-local-scope
标签: javascript function variables