传单层控制:订购 [英] Leaflet Layer Control: Ordering

查看:58
本文介绍了传单层控制:订购的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Leaflet中的标准图层控件" .实例如下:

We use the standard "layer control" from Leaflet. Instantiation is as follows:

this.controls[id] = L.control.layers({}, {}, {
   collapsed: false
});

层是动态添加的,并且是随机顺序的(取决于xhr请求何时完成).但是,似乎传单隐式使用相应图层的_leaflet_id在图层控件中进行排序,因此我们的图层以随机顺序出现.

Layers are added dynamically and in random order (depending on when xhr requests complete). However it seems that leaflet implicity uses the _leaflet_id of the respective layer for ordering in the layer control and thus our layers appear in random order.

有什么方法可以告诉传单在控件中应按哪个顺序显示图层?例如.通过在调用.addOverlay.addBaseLayer方法时传递一些附加参数?

Is there any way to tell leaflet in which order it should display the layers in the control? E.g. by passing some additional parameter when calling the .addOverlay or .addBaseLayer methods?

传单版本为1.0-dev

Leaflet version is 1.0-dev

推荐答案

我找到了解决该问题的一些方法.但是,这不是一个干净的解决方案,请随时发布更好的答案.

I found some hack-fix for the problem. However this is not a clean solution so feel free to post better answers.

Leaflet使用图层对象的_leaflet_id进行排序.因此,首先创建的图层将始终位于最上面.为了对图层进行排序,您必须将实际图层包装在图层/功能组中.

Leaflet uses the _leaflet_id of the layer object for sorting. Thus the layer created first will always be on top. In order to sort the layers you will have to wrap the actual layer in a layer/feature group.

|- dummy layer group
   |- some dummy geojson layer
   |- your actual layer (will be added later)

以编程方式:

var oFeatureGroup = L.featureGroup([
    L.geoJson({
        "type": "FeatureCollection",
        "features": []
    })
]);

然后我们进行一些异步加载,一旦准备好,就将图层添加到-或多或少的空-图层组中:

Then we do some async loading and once we are ready we add the layer to the - more or less empty - layer group:

oFeatureGroup.addLayer(oMyMagicAsyncLoadedLayer)

然后我们可以将图层添加到图层控件中,并将其分类为预期",其中顺序取决于oFeatureGroup图层而不是oMyMagicAsyncLoadedLayer的创建顺序.

We can then add the layer to the layer control and it will be sorted as "expected" where the order depends on the order of creation of the oFeatureGroup layers instead of the oMyMagicAsyncLoadedLayer.

这篇关于传单层控制:订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆