是否动态添加要素组到图层控件或从图层控件中删除要素组? [英] Dynamically adding/removing FeatureGroup to/from the Layer Control?

查看:98
本文介绍了是否动态添加要素组到图层控件或从图层控件中删除要素组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用leaflet.draw插件绘制多边形和线条.该插件依赖FeatureGroup进行存储和编辑.我想像图层控件中那样管理这些几何(例如,打开或关闭它们的可见性).

I am currently using leaflet.draw plugin to draw polygons and lines. The plugin depends on FeatureGroup for storing and editing. I want to manage these geometries (e.g. turn their visibility on or off) like those in the layer control.

最终,我希望能够添加和删除这些功能组.

Ultimately, I want to be able to add and remove these FeatureGroups.

这可以实现吗?

这是我要实现的目标: 1.将FeatureGroup视为动态图层(可以添加或删除或编辑).在这里我可以绘制几何图形并放置标记 2.添加FeatureGroup后,它应该位于图层控件"中,以便用户可以打开或关闭可见性.

Here is what I want to achieve: 1. Treat FeatureGroup like dynamic layers (can add or remove or edit). It is where I can draw geometries and place markers 2. When a FeatureGroup is added, it should be in the Layer Control so user can turn the visibility on or off.

推荐答案

是的,这是可能的.您可以将任何类型的图层添加到L.Control.Layers,这也意味着对图层进行分组:

Yes, that's possible. You can add any type of layer to L.Control.Layers, that also means grouping layers:

new L.Control.Layers(null, {
    'Markers': new L.FeatureGroup([
        new L.Marker([45, 0]),
        new L.Marker([-45, 0]),
        new L.Marker([0, 45]),
        new L.Marker([0, -45])
    ]),
    'Polylines': new L.FeatureGroup([
        new L.Polyline([[-45, -45], [45, 45]]),
        new L.Polyline([[45, -45], [-45, 45]])
    ])
}).addTo(map);

在Plunker上的工作示例: http://plnkr.co/edit/6mC6HTfPmzG9AblK1wyg?p=preview

Working example on Plunker: http://plnkr.co/edit/6mC6HTfPmzG9AblK1wyg?p=preview

这篇关于是否动态添加要素组到图层控件或从图层控件中删除要素组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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