简体   繁体   English

Leaflet layerGroup控件

[英]Leaflet layerGroup controls

I have a series of Leaflet FeatureGroups that are made up of a series of GeoJSON layers. 我有一系列由一系列GeoJSON图层组成的Leaflet FeatureGroups。 The FeatureGroups are conceptually similar but I need them to be separate for certain control reasons. FeatureGroup在概念上类似,但出于某些控制原因,我需要将它们分开。 I also need to be able to turn them all on and off at once. 我还需要能够立即打开和关闭它们。 Is there a way to do this? 有没有办法做到这一点?

I looked in the documentation and couldn't find an event that fires when the FeatureGroup is switched on and off. 我查看了文档,找不到FeatureGroup打开和关闭时触​​发的事件。 There is also no documented way of lumping the FeatureGroups into some kind of superGroup. 还没有记录的方法将FeatureGroups集中到某种类型的superGroup中。

For those who want to picture it, here is the workflow: 对于那些想要想象它的人来说,这是工作流程:

GeoJSON gets data that is turned into layers in Leaflet. GeoJSON获取在Leaflet中转换为图层的数据。 This is of different administration boundaries (eg States, Counties, etc...). 这是不同的行政边界(例如国家,县等)。 Each of the layers goes into a different FeatureGroup based on its type (eg Arkansas and New York go into the State FeatureGroup, Ford and Lincoln counties go into the County FeatureGroup). 每个图层根据其类型进入不同的FeatureGroup(例如,阿肯色州和纽约进入State FeatureGroup,福特和林肯县进入County FeatureGroup)。 This way I have control over opacity and styling for the different FeatureGroups (eg when I'm looking at the Counties of a state, I can lower the opacity of all the other states). 这样我可以控制不同FeatureGroup的不透明度和样式(例如,当我查看状态的县时,我可以降低所有其他状态的不透明度)。 I also need a way of turning all of this off and back on again. 我还需要一种方法来关闭所有这些并重新开启。 Leaflet provides the ability to do that on a FeatureGroup by FeatureGroup basis, but not a super set of that. Leaflet提供了基于FeatureGroup在FeatureGroup上执行此操作的功能,但不是超集。

Any ideas on how to achieve this? 关于如何实现这一点的任何想法?


New version of question: 新版问题:

What is the event that fires when turning a LayerGroup On and Off? 打开和关闭LayerGroup时触发的事件是什么? Is there anyway to hook into that? 无论如何都要加入那个?

I ended up having my usual LayerGroups or FeatureGroups, and then a FeatureGroup that contains all the layers on all the groups. 我最终得到了我常用的LayerGroups或FeatureGroups,然后是一个包含所有组中所有图层的FeatureGroup。 So when you add a shape or layer to a FeatureGroup, also add it to the FeatureGroup that you are using to keep track of everything. 因此,当您向FeatureGroup添加形状或图层时,还要将其添加到用于跟踪所有内容的FeatureGroup中。 And of course if you remove don't forget to remove it. 当然,如果你删除不要忘记删除它。 You can add this base FeatureGroup to the map along with the other groups and it should be fine. 您可以将此基本FeatureGroup与其他组一起添加到地图中,它应该没问题。

I don't think Leaflet currently provides an event when a LayerGroup is turned on or off (you are talking about with the L.Control, right?). 我不认为Leaflet当前打开或关闭LayerGroup时会提供一个事件(你正在谈论L.Control,对吧?)。 I agree that it would be useful. 我同意它会有用。 For now, you can just extend the code to do whatever you need to be done. 现在,您只需扩展代码即可完成您需要完成的任务。 For example: 例如:

var customLayerControl = L.Control.Layer.extend({
  _onInputClick: function(Layer, name){ 
      // This is just like calling super() if this confuses you!
      L.Control.Layers.prototype._onInputClick.call(this,Layer,name);
      // Do stuff
    }
});

Then instead of using the L.Control.Layers, use your custom layer control: 然后使用自定义图层控件而不是使用L.Control.Layers:

map.addControl(new customLayerControl({}, {'Custom Layer':customLayer},{}));

I hope this helps. 我希望这有帮助。

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

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