【问题标题】:Leaflet open a popup on marker click from GeoJSON layerLeaflet 在从 GeoJSON 层单击标记时打开一个弹出窗口
【发布时间】:2018-04-20 10:56:11
【问题描述】:

我制作了传单地图。 使用 GeoJSON 加载数据 在 onEachFeature 事件中,我已将弹出窗口与从该功能的某些属性中获取的 动态内容 文本绑定。

问题是我编写的代码仅在第一次点击时才会显示弹出窗口,然后我再次点击未显示的相同标记。

代码如下:

function showMarkets() {
    $.ajax({
        url: '/API/GetMarketsWithStatus',
        dataType: 'json',
        async: true,
    }).done(function (geoData) {
        mapLayers.markets = L.geoJSON(geoData, {
            onEachFeature: onEachMarketFeature
        }).addTo(map);
    });
}
function onEachMarketFeature(feature, layer) {
    layer.on('click', function (e) {
        layer.bindPopup('<a href="http://some-url-to-call?mktid=' + feature.properties.code + '">' + feature.properties.name + '</a>');
        this.openPopup();
    });
}

【问题讨论】:

    标签: javascript leaflet


    【解决方案1】:

    在创建功能时绑定您的弹出窗口,而不是在用户点击它时,Leaflet 将为您处理点击事件:

    function onEachMarketFeature(feature, layer) {
        layer.bindPopup('<a href="http://some-url-to-call?mktid=' + feature.properties.code + '">' + feature.properties.name + '</a>');
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      相关资源
      最近更新 更多