【问题标题】:Leaflet polygons sorting传单多边形排序
【发布时间】:2015-11-20 16:12:44
【问题描述】:

有没有办法,现在如何使用 Leaflet 对多边形进行 z 索引?启动地图时我没问题,但是当向现有地图添加新多边形时,我需要根据其面积将新多边形分类到现有多边形中 - 因此较大的多边形不会与较小的多边形重叠。我找到了这个解决方案:

Leaflet z-index

但是当我的地图包含更多的特征时,它的速度非常慢。有什么想法吗?

【问题讨论】:

    标签: sorting leaflet z-index polygon


    【解决方案1】:

    我不知道这个答案的性能,但你可以试一试:

    GeoJSON Layer Order In Leaflet 0.7.5

    主要代码是:

    // To be called after adding a geoJsonLayer to the map.
    function assignZindex(geoJsonLayer) {
        geoJsonLayer.eachLayer(function (layer) {
            layer._container.zIndex = layer.options.zIndex;
        });
    }
    
    // To be called after assignZindex().
    function reOrderVectorLayers() {
        var root = map._pathRoot,
            child = root.firstChild,
            next;
    
        while (child) {
            next = child.nextSibling;
            if (!next) {
                break;
            }
            if (next.zIndex < child.zIndex) {
                root.insertBefore(next, child);
                if (next === root.firstChild) {
                    continue;
                }
                child = next.previousSibling;
                continue;
            }
            child = next;
        }
    }
    

    它假定zIndex 属性是在每个矢量(多边形)层的options 中定义的数字。

    正如该问题的 cmets 所述,如果您使用 Leaflet 1.x,您现在可以创建自己的窗格,您可以通过 CSS z-index 订购这些窗格,并将每个矢量图层插入指定的窗格中。

    【讨论】:

    • 谢谢,但这正是我在问题中链接的文章的原理 - 使用 DOM 模型处理对象。
    • 确实很相似,因为它使用 DOM 对向量进行排序,但实现方式不同。意识到 DOM 排序是唯一确保 Leaflet 0.x 中 SVG 元素(因此是多边形)的特定顺序的方法,除非您准备好修改 Leaflet 以使其行为类似于 Leaflet 1。 x(用于创建自定义窗格)。
    猜你喜欢
    • 2015-01-09
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多