Leaflet:在一个层内排序 GeoJSON 元素

Leaflet : ordering GeoJSON elements inside a layer

我正在使用带有 pointToLayer 函数的传单显示 GeoJSON 图层。到目前为止一切正常。

但我想根据 GeoJSON 的 属性 按特定顺序显示我的点数。这很重要,因为我的点的半径会随着这个 属性 而变化,我需要在顶部显示较小的圆圈。我希望我说清楚。

我已经尝试了很多东西,但这是我认为最好的尝试:

var pointLayer = L.geoJson(centroids, {
                pointToLayer: function (feature, latlng) {
                    return L.circleMarker(latlng, {
                        fillColor: "#76C551",
                        color: "#000",
                        weight: 1,
                        fillOpacity: 1
                    });
                },
                onEachFeature: function (feature, layer) {
                    var radius = calcPropRadius(feature.properties.nb);
                    layer.setRadius(radius);
                    feature.zIndexOffset = 1/feature.properties.nb*1000;
                    layer.bindPopup(feature.properties.name + " : " + String(feature.zIndexOffset));
                }
            });

您可以注意到可以在弹出窗口中读取功能的 zIndexOffset,看起来还不错。但是圆圈的显示顺序并不反映 zIndexOffset。 我试过使用 setZIndexOffset 方法,但据我了解它只适用于标记。

有人知道怎么做吗?非常感谢任何见解!

如您所知,zIndexOffset 选项仅适用于 L.marker

L.circleMarker 进入 overlayPane and you can re-order them one to each other using .bringToFront() and .bringToBack() 方法。

虽然 ghybs 答案非常适用于 leaflet 0.7,但切换到 leaflet 1.0 允许使用窗格,这使得解决方案更简单:

var pointLayer = L.geoJson(centroids, {
            pointToLayer: function (feature, latlng) {
                return L.circleMarker(latlng, {
                    fillColor: "#76C551",
                    color: "#000",
                    weight: 1,
                    fillOpacity: 1
                });
            },
            onEachFeature: function (feature, layer) {
                var radius = calcPropRadius(feature.properties.nb);
                layer.setRadius(radius);
                layer.setStyle({pane: 'pane'+ feature.properties.nb});
                var currentPane = map.createPane('pane' + feature.properties.nb);
                currentPane.style.zIndex = Math.round(1/feature.properties.nb*10000);
                layer.bindPopup(feature.properties.name + " : " + String(feature.zIndexOffset));
            }
        });

希望对其他人有用!