我有以下代码循环遍历一组标记;
地点数组:
var map_markers = [['Windsor Road, Salisbury, SP2 7NF <br/> 2nd Line', 'Windsor Road, Salisbury,', 1],['Bishopdown Road, Salisbury, SP1 3DT <br/> 2nd Line', 'Bishopdown Road, Salisbury,', 2],['Gainsborough Close, Salisbury, SP2 9HD <br/> 2nd Line', 'Gainsborough Close, Salisbury,', 3],['Montgomery Gardens, Salisbury, SP2 7UQ <br/> 2nd Line', 'Montgomery Gardens, Salisbury,', 4],['Manor Court, Salisbury, SP1 1LN <br/> 2nd Line', 'Manor Court, Salisbury,', 5],];
显然,以上所有内容都需要地理编码,以便我可以在地图上为每个人放置一个图钉,为此我构建了以下函数:
function init_map(map_id, center_address, markers) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': center_address, 'region': 'uk' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//Init Map Options
var mapOptions = {
zoom: 11,
center: results[0].geometry.location,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById(map_id), mapOptions);
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < markers.length; i++) {
var marker_address = markers[i][1];
var marker_content = markers[i][0];
geocoder.geocode( { 'address': marker_address , 'region': 'uk' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(marker_content);
infowindow.open(map, marker);
}
}) (marker, i));
} else {
alert("There was a issue getting the property information.")
}
});
}
} else {
alert("Unable to find address: " + status);
}
});
}
使用上面的代码,标记被正确放置在地图上,当我单击标记时,infoWindow 确实出现了,但是,它似乎卡在循环中,并且只在所有标记上显示最终的数组元素内容。任何人都可以推荐一个修复?