I'm using draw plugin in my project and I would like to know how can I hide/show drawing tools by layer type?
For example, let's say I have 2 layers one of them type is Polygon and the other one is Line.
If user select Polygon layer, I want to hide Line drawing tool.
After that, If user select Line layer, I want to hide Polygon drawing tool. I've looked here but this example is making tools static, I want to change dynamically. How can I do that?
Every suggestion will be appreciated.
I solved it myself. I'm adding this draw control when map initialized.
drawControl = new L.Control.Draw({
draw : {
position : 'topleft',
polygon : false,
polyline : false,
rectangle : false,
circle : false
},
edit : false
});
map.addControl(drawControl);
After that, i wrote a function for resetting drawing tools.
function setDrawingTools(layerType) {
map.removeControl(drawControl);
if (layerType == 'Polygon') {
drawControl = new L.Control.Draw({
draw : {
position : 'topleft',
polygon : {
title : 'Draw a sexy polygon!',
allowIntersection : false,
drawError : {
color : '#b00b00',
timeout : 1000
},
shapeOptions : {
color : '#bada55'
},
showArea : true
},
polyline : false,
rectangle : false,
circle : false,
marker : false
},
edit : false
});
} else if (layerType == 'Line') {
drawControl = new L.Control.Draw({
draw : {
position : 'topleft',
polygon : false,
polyline : {
metric : false
},
rectangle : false,
circle : false,
marker : false
},
edit : false
});
} else if (layerType == 'Point') {
drawControl = new L.Control.Draw({
draw : {
position : 'topleft',
polygon : false,
polyline : false,
rectangle : false,
circle : false
},
edit : false
});
}
map.addControl(drawControl);
}
It appears you can't do that with the plugin, but you can use CSS to show/hide certain drawing tools when you switch layers.
The buttons have classes like leaflet-draw-draw-polyline
, leaflet-draw-draw-polygon
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With