我已经和infowindow.setContent(address);
进行了几个小时的摔跤。根据我在这里读到的内容,我知道它必须对闭包做一些事情,但我不能让它起作用。我的两个信息窗口现在都是一样的。
这是现在的测试网站,对不起的外观感到抱歉:)。
http://testsite.edwardgranger.com/2013/12/05/hello-world/
PHP& js代码:
<script type="text/javascript">
var o = <?php echo json_encode($m_userArray); ?>
</script>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker;
var i;
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
for(var i = 3; i <= 4; i++) {
var address = window.o[i].address;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(address);
infowindow.open(map, marker);
}
})(marker, i));
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
我尝试过几个不同的概念,但说实话,我今晚才知道闭包,我在更高层次上理解它们,但是尝试导航我的代码是件苦差事,第二组眼睛现在很棒。