问题
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