2

我对javascript很陌生,我正在学习,如果这很简单,很抱歉。

我所拥有的是在地图上显示的一堆标记,这些标记是从一个数组中加载并用一个函数显示的。我想要做的是弹出一个与单击的标记相关的特定 div。单击另一个标记时,前一个 div 关闭,新 div 打开。

这是我到目前为止所拥有的,以了解我在做什么。

我想我想写一个函数说......“如果点击'标记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>
4

1 回答 1

1

您需要在标记和 div 之间建立关系,目前没有。

代替像div-sg这样的ID 可以是例如div0,其中数字指示标记的索引。

那么访问div就没有问题了:

   //access the node
 var content=document.getElementById('div'+i)
   //create a copy of the node
 .cloneNode(true);
   //remove the id to avoid conflicts
 content.removeAttribute('id');
   //make it visible  
 content.style.display='block';
   //apply the content  and open the infoWindow 
 infowindow.setContent(content);
 infowindow.open(map, marker);
于 2013-01-17T21:43:19.123 回答