我正在添加两个Leaflet Draw(https://github.com/Leaflet/Leaflet.draw)实例(仅使用直线):
var drawControl = new L.Control.Draw({
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl);
var drawControl2 = new L.Control.Draw({
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl2);
现在我想听取draw:drawvertex
事件,并根据我是否已激活drawControl
或drawControl2
来执行不同的操作:
map.on('draw:drawvertex', function (e) {
console.log("Vertex drawn", e);
});
如何区分哪个drawControl当前处于活动状态?
答案 0 :(得分:1)
这是一种了解哪种drawControl处于活动状态的脏方法。
诀窍是将它们放在不同的地图角落。它有助于检查用户绘制时可见的ul.leaflet-draw-actions
。 div.leaflet-top
中的那个或div.leaflet-bottom
中的那个:
var drawControl = new L.Control.Draw({
position: 'topleft',
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl);
var drawControl2 = new L.Control.Draw({
position: 'bottomleft',
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl2);
map.on('draw:drawvertex', function (e) {
console.log("Vertex drawn", e);
if ($('div.leaflet-top ul.leaflet-draw-actions').is(':visible')){
console.log('it was drawn with drawControl');
}
else {
console.log('it was drawn with drawControl2 !');
}
});
很脏但是有效。