我正在使用谷歌地图api v3开发一个项目,并且当我在infowindow上点击一个标记时,我正试图这样做。我已经完成了这个,但现在我想要的是在点击不同的标记时显示的不同内容,我似乎无法弄清楚。任何帮助表示赞赏。谢谢!
function initialize() {
var locations = [
['Phi Delta Theta', 39.511747,-84.735117],
['Pi Kappa Alpha', 39.511776, -84.735684],
['Sigma Nu', 39.513921, -84.735159],
['Sigma Alpha Epsilon', 39.514332, -84.734956],
['Beta Theta Pi', 39.5107519, -84.738549]
];
var myLatlng = new google.maps.LatLng(39.511747,-84.735117);
var mapOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var contentString = '<div id="content">'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
答案 0 :(得分:0)
Here is the answer that does what you want,但它允许一次打开多个infoWindow。 重点是定义windowinfo的多个实例。 而不是:
var infowindow = new google.maps.InfoWindow({
content: contentString
});
....
for (i = 0; i < locations.length; i++) {
}
像这样做:
for (i = 0; i < locations.length; i++) {
var infowindow = new google.maps.InfoWindow({
content: contentString
});
}
这样您就可以定义多个标记,每个标记都有不同的内容。