我正在制作一个关于在挪威遇害的骑车人的网站。对于我的项目,我一直在使用谷歌地图api v3,但我对javascript有着模糊的熟悉。到目前为止,您可以在此处查看我的结果:http://salamatstudios.com/googlemapstest/
基本上我想在每个标记上都有多个带有infowindows的标记。每个infowindows将包含: 姓名年龄), 地点, 死亡日期, 阅读更多内容(将链接到网站上的页面)。
就像这个例子:http://salamatstudios.com/bicycles/
我尝试使用一个标记和infowindow,并且工作得很好。当我想添加带有自定义信息窗口的新标记时,我就会卡住。目前,我在第一个链接中看到了不同位置的3个标记,但是当我点击标记时没有出现任何信息窗口。
我如何绕过它来编码它以便出现信息?我怎样才能在每个infowindow中都有自定义文本?完成后,我将在地图上有大约30-40个标记。所有信息窗口都有不同类型的信息。
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(65.18303, 20.47852),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP,
// MAP CONTROLS (START)
mapTypeControl: true,
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.LEFT_TOP
},
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_TOP
},
// MAP CONTROLS (END)
};
var map = new google.maps.Map(document.getElementById("map"),
mapOptions);
// -------------- MARKER 1
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(59.96384, 11.04120),
map: map,
icon: 'img/bike5.png'
});
// MARKER 1'S INFO WINDOW
var infowindow1 = new google.maps.InfoWindow({
content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
});
// End of infowindow code
// Adding a click event to the marker
google.maps.event.addListener(marker1, 'click', function() {
// Calling the open method of the infoWindow
infowindow1.open(map, marker);
});
// -------- END OF 1st MARKER
// -------------- MARKER 2
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(60.63040, 8.56102),
map: map,
icon: 'img/bike5.png'
});
// MARKER 2'S INFO WINDOW
var infowindow2 = new google.maps.InfoWindow({
content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
});
// End of infowindow code
// Adding a click event to the marker
google.maps.event.addListener(marker2, 'click', function() {
// Calling the open method of the infoWindow
infowindow2.open(map, marker);
});
// -------- END OF 2nd MARKER
// -------------- MARKER 3
var marker3 = new google.maps.Marker({
position: new google.maps.LatLng(60.39126, 5.32205),
map: map,
icon: 'img/bike5.png'
});
// MARKER 3'S INFO WINDOW
var infowindow3 = new google.maps.InfoWindow({
content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
});
// End of infowindow code
// Adding a click event to the marker
google.maps.event.addListener(marker3, 'click', function() {
// Calling the open method of the infoWindow
infowindow3.open(map, marker);
});
// -------- END OF 3rd MARKER
}
google.maps.event.addDomListener(window, 'load', initialize);
如果有人能给我一些线索,那会很棒。我试过一下,但我找不到答案。提前致谢! : - )
答案 0 :(得分:9)
您需要将infowindow附加到正确的标记。目前它们都与“标记”相关联,而“标记”不存在(并且当您单击标记时,应该在javascript控制台中导致错误消息)。
点击监听器更改内部:
infowindow1.open(map, marker);
infowindow2.open(map, marker);
infowindow3.open(map, marker);
要:
infowindow1.open(map, marker1);
infowindow2.open(map, marker2);
infowindow3.open(map, marker3);
答案 1 :(得分:2)
除了HoangHieu答案,当你使用for循环时,最好以这种方式使用它:
marker.info = new google.maps.InfoWindow({
content: 'some text'
});
google.maps.event.addListener(marker, 'click', function() {
this.info.open(map, this);
});
答案 2 :(得分:1)
google.maps.event.addListener(marker1, 'click', function() {
// Calling the open method of the infoWindow
infowindow1.open(map, marker);
});
更改为
google.maps.event.addListener(marker1, 'click', function() {
// Calling the open method of the infoWindow
infowindow1.open(map, this);
});