【问题标题】:Making a Google Maps marker show a specific div when clicked使 Google Maps 标记在单击时显示特定的 div
【发布时间】: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


【解决方案1】:

你需要在marker和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);

【讨论】:

  • 谢谢,这很有意义!我现在可以让它们打开和关闭正确的 div。我遇到的问题是创建一个语句,告诉我打开的 div 在单击另一个标记时关闭,而是打开新的相应标记。对此有何见解?
  • 我没有您当前的代码,但是当您使用我在brunchtoronto.com/indexA.html 中发布的代码时,无需进一步声明。只有 1 个 infoWindow,当它打开并单击另一个标记时,它将再次打开并更新内容和位置。
  • 对,但我希望外部 DIV 的行为就像 infoWindows 一样,因为当您单击另一个标记时,当前 DIV 隐藏,新 DIV 显示。您的代码完美运行,唯一没有做的是隐藏单击新标记时打开的当前 DIV。我在哪里包含 content.style.display='none';?
  • Craig Morrison:如果您只需要更新地图之外的 div。只需使用信息窗口内容更新您的 div。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 2013-01-25
  • 1970-01-01
  • 2023-03-27
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多