【问题标题】:Google maps: infowindow is not closing谷歌地图:信息窗口没有关闭
【发布时间】:2012-10-08 20:00:01
【问题描述】:

当我单击另一个标记时,我的信息窗口不会自动关闭.. 这里的代码:http://pastebin.com/PvCt2z7W 这是带有 infowindows 的标记的代码

 for (var n = 0 ; n <listFavourite.length ; n++)
                    //var favouriteObject =listFavourite[n];
                       addMarker(listFavourite[n]);
                   }           
                   function addMarker(data) {
                       var marker = new google.maps.Marker({
                           position: new google.maps.LatLng(data.lattitude, data.longitude),
                           map: map,
                           title: data.address
                       });

                       var contentString = '<div id="content">'+
                           '<div id="siteNotice">'+
                           '</div>'+
                           '<h2 id="firstHeading" class="firstHeading">'+data.name+'</h2>'+
                           '<div id="bodyContent">'+
                           '<p>'+data.address+'</p>'+
                           '<p></p>'+
                           '<p>Do You Want to change search location</p>'+
                           '<input name="yes" id="yes" type="button" class="btn-common" value="Yes"/>'+
                           '</div>'+
                           '</div>';                                                   
                       var infowindow = new google.maps.InfoWindow({
                       maxWidth: 10,
                       });              
                       google.maps.event.addListener(marker, "click", function() {                     
                           infowindow.setContent(contentString);
                           infowindow.open(map, marker);                       
                       });     
                   };

我在这里尝试了所有答案,但无法正常工作 任何建议或帮助将不胜感激

【问题讨论】:

    标签: google-maps-api-3 infowindow


    【解决方案1】:

    不是为每个标记创建一个信息窗口,而是有一个全局信息窗口变量。然后,您在标记单击处理程序中所做的一切就是每次更新标记的内容。但是,您的代码将要求使用闭包来完成此操作,否则您将获得具有最后一个标记内容的每个标记。

    var infowindow = new google.maps.InfoWindow({
       maxWidth: 10
    });
    
    var arrMarkers = [];
    
    function addMarker(data) {
       var marker = new google.maps.Marker({
           position: new google.maps.LatLng(data.lattitude, data.longitude),
           map: map,
           title: data.address
       });
    
        arrMarkers.push(marker);
    
       var contentString = '<div id="content">'+
           '<div id="siteNotice">'+
           '</div>'+
           '<h2 id="firstHeading" class="firstHeading">'+data.name+'</h2>'+
           '<div id="bodyContent">'+
           '<p>'+data.address+'</p>'+
           '<p></p>'+
           '<p>Do You Want to change search location</p>'+
           '<input name="yes" id="yes" type="button" class="btn-common" value="Yes"/>'+
           '</div>'+
           '</div>';                                                   
    
    
        // add an event listener for this marker
        bindInfoWindow(marker, map, infowindow, contentString);   
    }
    
    
    function bindInfoWindow(marker, map, infowindow, html) { 
        google.maps.event.addListener(marker, 'click', function() { 
            infowindow.setContent(html); 
            infowindow.open(map, marker); 
        }); 
    }
    
    function clickLink(ID) {
        google.maps.event.trigger(arrMarkers[ID], 'click');
    }
    

    您的侧边栏链接 HTML 可能如下所示:

    <a href="javascript:clickLink(0); return false;">Link 1</a><br>
    <a href="javascript:clickLink(1); return false;">Link 2</a><br>
    ...
    

    【讨论】:

    • yeahh 经历过它为所有标记获得相同的地址.. JS 的新手,您能否在“关闭”以及如何使用我的代码方面提供更多帮助
    • 当我点击侧边栏链接时我是否可以打开窗口...实际上我在侧边栏上显示了一些保存的结果..
    • 您需要做的是在任何标记上触发点击事件。为此,您需要将标记存储在一个数组(全局变量)中,并且侧边栏链接调用一个 JS 函数,传递一个整数,该整数表示对应于数组的哪一行(即哪个标记),记住 JS 数组开始在 0 而不是 1。我已经用更多代码更新了我的答案来说明这一点
    • 嗨,我正在尝试在 infowindow 中实现单击事件 ..但是警报语句没有触发...html 对我来说看起来不错 ..这是行:
    • 不确定,我从未尝试从信息窗口中调用 javascript
    【解决方案2】:

    使用函数闭包的另一个选项(基本上涉及使用 createMarker 函数)是将信息窗口的内容保存在标记的属性中。使用单个信息窗口仍然是最简单的。您没有在您的 pastebin 中提供足够的上下文来为您提供一个可行的示例。

    这是一个使用函数闭包将信息窗口内容与标记相关联的示例,并且具有单个信息窗口(翻译自 Mike Williams 的 v2 教程)。

    http://www.geocodezip.com/v3_MW_example_map3.html

    Mike Williams' description of function closure

    这是一个不使用函数闭包的示例(它将信息窗口内容存储在标记对象的属性中):

    http://www.geocodezip.com/v3_MW_example_map3_noclosure.html

    【讨论】:

    • @geocodezip...试过但完全困惑..这是我的js文件pastebin.com/ghRPnKpB
    • 感谢 geocodezip 的链接。终于了解了我以前不知道的闭包。
    • 我正在尝试在 v3 中实现您的示例..在侧边栏上显示结果,当我单击特定标记将打开的链接时..
    • 我看到了你的 pastebin。它不完整,如果不做你已经完成的一堆工作,我不能用它来制作一个工作示例。我不明白您的说法“我正在尝试在 v3 中实现您的示例”,我的示例在 v3 中。
    • 使用不使用函数闭包的教程页面版本更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2014-04-06
    • 2013-02-05
    相关资源
    最近更新 更多