我的代码完成了我想要的所有内容,除非您必须单击标记才能打开窗口。如何在加载时打开窗口?我似乎无法弄清楚我做错了什么。我的代码中有info.window(map,marker)。我没有正确使用它吗?
function initialize() {
var mapOptions = {
panControl: false,
scrollwheel: false,
scaleControl: true,
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_BOTTOM
},
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DEFAULT,
position: google.maps.ControlPosition.LEFT_CENTER
}
};
var image = 'images/ventible-marker.png';
var map = new google.maps.Map(document.getElementById('map-page'), mapOptions);
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
for (var i in LocationData) {
var p = LocationData[i];
var latlng = new google.maps.LatLng(p[0], p[1]);
bounds.extend(latlng);
var marker = new google.maps.Marker({
map:map,
position: latlng,
icon: image,
animation: google.maps.Animation.DROP, cursor: "default"
});
var content = "<ul class='list-inline'>" +
"<li>" +
"<a href='event.php?page=" + p[2].replace(/\s/g, "-") + "'>" +
"<img src='" + p[4] + "'/>" +
"</a>" +
"</li>" +
"<li>" +
"<a href='event.php?page=" + p[2].replace(/\s/g, "-") + "'>" +
"<strong>" + p[2] + "</strong></a>" +
"<br/>" + p[5] +
"<br/><a href=\"https://maps.google.com/maps?daddr=" +
p[3] + "\" target=\"_blank\">Get Directions</a>" +
"</li>" +
"</ul>";
google.maps.event.addListener(marker, 'click', (function(marker, content) {
return function() {
infowindow.setContent(content);
infowindow.open(map, marker);
}
})(marker, content));
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:1)
你有电话
infowindow.open(map, marker);
仅在标记click
事件的事件侦听器中。如果要在加载时打开infowindow,则必须在创建内容后添加相同的代码:
var content = ...
infowindow.setContent(content);
infowindow.open(map, marker);
仅更改您将为最后一个标记打开信息窗口。因此,您必须将infowindow的创建移至for loop
:
var bounds = new google.maps.LatLngBounds();
//var infowindow = new google.maps.InfoWindow();
for (var i in LocationData) {
var infowindow = new google.maps.InfoWindow();
...
如果没有图片且没有您的内容,请参阅example at jsbin。