【发布时间】:2013-01-17 20:43:18
【问题描述】:
我对 javascript 还很陌生,我正在学习,如果这很简单,很抱歉。
我所拥有的是在地图上显示的一堆标记,这些标记是从一个数组中加载并与一个函数一起显示的。我想要做的是弹出一个与单击的标记相关的特定 div。当点击另一个标记时,之前的 div 会关闭,新的 div 会打开。
This is what I have so far,了解我在做什么。
我想我想写一个函数说......“如果点击'标记A',打开'div A',如果在'标记A'打开时点击'标记B',然后关闭'Div A' 并打开 'Div B'。
这是我的 javascript。
var markers = [
['Saving Grace', 43.652659,-79.412284],
['Starving Artist', 43.660281,-79.443570]
];
// Standard google maps function
function initialize() {
var myLatlng = new google.maps.LatLng(43.655826,-79.383116);
var image = 'http://www.brunchtoronto.com/images/marker-blue.png';
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Current Toggle function which displays Feature Box when marker is clicked
function opendiv() {
var ele = document.getElementById("div-feature");
ele.style.display = "block";
}
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < markers.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(markers[i][1], markers[i][2]),
map: map,
icon: image
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
map.panTo(marker.getPosition());
infowindow.setContent(markers[i][0]);
infowindow.open(map, marker);
opendiv();
}
})(marker, i));
}
}
还有我的 HTML
<!-- Featued Window -->
<div class="featured_window" id="div-feature" style="display: none">
Stuff to display
</div>
<!-- Saving Grace -->
<div class="featured_window" id="div-sg" style="display: none">
Stuff to display
</div>
【问题讨论】:
-
您在哪里可以获得有关标记的信息?您的地图标记与您要显示的信息之间的联系是什么?您可能希望将标记 Id 传递给 openDiv 函数并从那里显示适当的信息,具体取决于单击的标记。使用相同的 div - 只需更新其内容。
标签: google-maps-api-3 google-maps-markers show-hide