简体   繁体   English

在需要时单击填充Leaflet弹出窗口

[英]Fill Leaflet popup on click when acctually needed

I have a map with a lot of markers and a complex popup content, generated by a function called popupcontent(), wich takes a lot of time to compute when it is done for all markers on the map with the oneachfeature function. 我有一个包含大量标记和复杂弹出内容的地图,由一个名为popupcontent()的函数生成,当使用oneachfeature函数对地图上的所有标记进行计算时,需要花费大量时间来计算。

Is there a way to triger a function in a pupop only when it is actually opened instead of generating all the popups in the beginning? 有没有办法在pupop中实现一个函数,只有当它实际打开而不是在开始时生成所有弹出窗口? This would speed up loading time a lot. 这会加快加载时间。

This is my code so far (I am using the markerclusterer extention): 到目前为止这是我的代码(我正在使用markerclusterer扩展):

    var geojson1 = L.geoJson(bigJson,{

    onEachFeature: function (feature, layer) {
        layer.bindPopup(popupcontent(feature,layer));
    }
    })
                .addLayer(tiles);
        var markers = L.markerClusterGroup({
            spiderfyOnMaxZoom: true,
            showCoverageOnHover: true,
            zoomToBoundsOnClick: true,
            disableClusteringAtZoom: 10,
            removeOutsideVisibleBounds:true
        });

        var geoJsonLayer = L.geoJson(bigJson, {
        });

        markers.addLayer(geojson1);
        map.addLayer(markers);
        map.fitBounds(markers.getBounds());

Demo: http://stefang.cepheus.uberspace.de/farmshops/ 演示: http//stefang.cepheus.uberspace.de/farmshops/

I think what you're looking for is something like so (if your layer is an interactive layer ): 我认为你正在寻找的是这样的(如果你的图层是一个交互层 ):

onEachFeature: function (feature, layer) {
  layer.once("click", ()=>{
    layer.bindPopup(popupcontent(feature,layer)).openPopup();
  });
}

Use "once" instead of "on" so it only gets binded once when the layer is clicked. 使用“一次”而不是“打开”,因此只有在单击图层时才会绑定一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM