問題描述
我在我的項目中使用繪圖插件,我想知道如何按圖層類型隱藏/顯示繪圖工具?
I'm using draw plugin in my project and I would like to know how can I hide/show drawing tools by layer type?
例如,假設我有 2 層,其中一層是多邊形,另一層是線.
For example, let's say I have 2 layers one of them type is Polygon and the other one is Line.
如果用戶選擇多邊形圖層,我想隱藏畫線工具.
之后,如果用戶選擇Line layer,我想隱藏多邊形繪圖工具.我看過 here 但這個例子使工具靜態化,我想動態地改變.我該怎么做?
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?
我們將不勝感激.
推薦答案
我自己解決了.我在地圖初始化時添加了這個繪制控件.
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);
}
這篇關于Leaflet Draw Plugin:如何按圖層類型動態隱藏/顯示繪圖工具的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!