【问题标题】:Leaflet popup does not open on the second click on the marker第二次单击标记时未打开传单弹出窗口
【发布时间】:2014-04-30 18:24:21
【问题描述】:

我正在使用 Leaflet.js 在 OSM 地图上显示标记。

问题是,第一次单击标记时,弹出窗口正常打开,但第二次单击同一标记时,弹出窗口不再打开。

PS:我在代码中的其他任何地方关闭弹出窗口(使用closePopup() 函数)。在下面的块中,我什至注释掉了单击标记后其他弹出窗口的显式关闭。
PPS:我的应用程序在 Ruby on Rails(ruby-1.9.3、Rails 3.2.16)上运行,并使用 leaflet-rails (0.7.2)

bindListeners = function(marker){
    marker.on('click', function(evt) {  

        //resize all markers' icons to default size
        for (i=0;i<markersOfTheMap.length;i++) {
            resizeMarkerIcon(markersOfTheMap[i], false);
        }
              //map.closePopup();

        var infoBoxContent = buildInfoboxHtml(marker);

        marker.bindPopup(infoBoxContent, {className: 'click-popup'}, {closeOnClick: false});
        resizeMarkerIcon(marker, true);
        marker.openPopup();


        var popup = marker.getPopup(); // returns marker._popup
        popup._isOpen = true; 
        console.log("is popup open? " +popup._isOpen); // true
        popupsTestArray.push(popup); 
        console.log(popupsTestArray); // popup_isOpen is false...
           });

【问题讨论】:

  • 我也遇到了同样的问题。你解决了吗?

标签: javascript ruby-on-rails popup leaflet


【解决方案1】:

我也遇到了同样的问题。我在这里粘贴我的一段代码。希望它可以帮助您解决问题。

marker.on('click', function (e) {
                            if (e.target._popup == undefined) { // same as e.target.getPopup()
                                $.getJSON(url, { entityObject: e.target.options.alt }, function (infoBoxContent ) { //e.target.options.alt contains entity Id from which we will get Infobox window content.
                                    e.target.bindPopup(infoBoxContent).openPopup();
                                });
                            }
                            else {
                                marker.openPopup();
                            }
                        });

在 If 条件下,我们从服务器端获取数据并将其绑定到标记弹出窗口。 在其他情况下,再次单击相同的标记后,我们将显示来自客户端的内容。

这段代码对我很好。如果您有任何疑问,我们可以在这里讨论。

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2021-03-29
    • 1970-01-01
    • 2020-02-16
    相关资源
    最近更新 更多