我正在尝试根据包含信息窗口文本和邮政编码的数据数组在地图上绘制多个点。我还添加了一个侦听器,以便在单击标记时弹出信息窗口。
当我运行以下代码时,只有最后一个标记被放置。有人可以帮忙吗?
<div class="GoogleMap">
<script src="http://maps.google.com/maps/api/js?sensor=false"type="text/javascript"></script>
<div id="map" style="width: 977px; height: 600px;"></div>
<script type="text/javascript">
var locations = [
['<h4>Test</h4>', 'CH45 4SQ'],
['<h4>Test2</h4>', 'CH63 9AL']
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: new google.maps.LatLng(53.45631, -2.73710),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
var marker = new google.maps.Marker({ map: map });
var i;
for (i = 0; i < locations.length; i++) {
geocoderRequest = {address: locations[i][1]}
geocoder.geocode(geocoderRequest, function(results, status) {
marker.setPosition(results[0].geometry.location);
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</div>