Dynamically adding/removing FeatureGroup to/from the Layer Control?

雨燕双飞 提交于 2020-01-06 03:15:53

问题


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.

Is this achievable?

Additional Information (thanks to iH8 for commenting and sorry for the lack of details):

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.


回答1:


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);

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



来源:https://stackoverflow.com/questions/34758868/dynamically-adding-removing-featuregroup-to-from-the-layer-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!