<分区>
我目前正在进行的一个项目需要实现一个带有多个标记和多个信息框的谷歌地图。引用 map API 这似乎是一个很好的起点:
https://developers.google.com/maps/documentation/javascript/examples/icon-complex
所以我使用此代码作为基础并以此为基础进行构建。现在我坚持的一点是为每个标记添加一个独特的信息框。这是我的来源
http://jsfiddle.net/jackthedev/asK5v/1/
如您所见,我正在尝试调用数组中选中对象的第一个元素,它非常适合纬度、经度和标题,而不是 contentstring 变量。
for (var i = 0; i < locations.length; i++) {
var office = locations[i];
var myLatLng = new google.maps.LatLng(office[1], office[2]); //works here
var infowindow = new google.maps.InfoWindow({content: contentString});
var contentString =
'<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">'+ office[0] + '</h1>'+ //doesnt work here
'<div id="bodyContent">'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({content: contentString});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: globalPin,
title: office[0], //works here
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,this);
});
}
要查看我要解释的内容,只需单击上面演示中的每个标记,您将看到一个标题为“中国”的信息框弹出窗口。由于某种原因,它会为每个标记捕获最后一个对象的第一个元素。
我想要实现的是,如果有意义的话,所有信息框对于那里的标记都是唯一的?因此,当我单击新加坡的标记时,将使用我之前定义的数组对象弹出一个带有新加坡标题的信息框。
谢谢,我希望我已经足够清楚了