我想做的是使用AJAX和JSON加载一堆地址,找到每个地址的纬度和经度,在地图上放置标记,然后使用
我已经完成了大部分工作,但我的问题在于
基本上,当循环遍历每个地址并查找纬度和经度时,似乎会在循环下面加载
所以它正在调用
以下是我的代码:
fitBounds()
缩放以便所有标记都可见。听起来很简单。我已经完成了大部分工作,但我的问题在于
fitBounds()
部分。基本上,当循环遍历每个地址并查找纬度和经度时,似乎会在循环下面加载
fitBounds()
函数。所以它正在调用
fitBounds()
,但还没有任何内容在marker_bounds
数组中。我本以为它会在到达fitBounds()
函数之前完成循环,但我想geocoder.geocode()
函数可能会导致它继续加载其余的JavaScript,同时确定纬度和经度。以下是我的代码:
var geocoder;
var map;
var marker_bounds = [];
var myOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
$.post('addresses.php', function(data) {
var next_filter = '';
for(var x=0;x<data.addresses.length;x++) {
geocoder.geocode( { 'address': data.addresses[x].address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var latLng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
marker_bounds.push(latLng);
}
});
}
var latlngbounds = new google.maps.LatLngBounds();
for ( var i = 0; i < marker_bounds.length; i++ ) {
latlngbounds.extend(marker_bounds[i]);
}
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
}, 'json');
在所有地理编码完成后,是否有可调用的函数?