首页 > 解决方案 > Leaflet - openPopup() 不显示 geoJSON

问题描述

我正在尝试将弹出窗口绑定到 geoJSON 层上的标记。为此,我使用了 onEachFeature 函数:

var onEachFeature = function(feature, layer) {
                    layer.bindPopup("hello",
                                   {closeButton:false,
                                    autoClose: false,
                                    closeOnClick: false,
                                    className: "popup-custom"}).openPopup();
}

这不会导致弹出显示。弹出窗口已创建,但我需要单击标记以显示它。在不单击标记的情况下使弹出窗口可见,我缺少什么?

我没有使用 pointToLayer 函数,因为我也在使用过滤器功能过滤特征并使用请求来自定义弹出窗口和标记(弹出窗口绑定实际上是在回调函数中)。

标签: javascriptleafletgeojson

解决方案


您会得到一个 geojsonlayer 作为结果,然后您可以打开每个图层的弹出窗口:

var geojsonLayer = L.geoJSON(data, {
    onEachFeature : onEachFeature 
}).addTo(map);

geojsonLayer.eachLayer(function(layer){
    layer.openPopup();
});

推荐阅读