【发布时间】:2011-06-02 07:39:04
【问题描述】:
当我在地理编码器函数之外提醒我的数组“markerArray”时,它说它未定义。
想不通为什么?有没有办法从函数外部的数组中获取值?
var markerArray = new Array();
for(var i in opts.markers)
{
address = opts.markers[i].address;
//alert(opts.markers[i].icon);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK && results.length) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
}
}
markerArray[i] = marker;
});
}
alert(markerArray[0].position);
【问题讨论】:
-
opts.markers是数组还是对象? -
这可能解决不了你的问题,但不要使用
for ... in枚举数组:bonsaiden.github.com/JavaScript-Garden/#array.general -
你能在 markerArray[i] = marker; 之后插入 console.log(markerArray)在警报之前?并显示结果。您可以在 firebug 或其他浏览器的 javascript 开发工具中监控 console.log 的输出。
标签: javascript function google-maps callback