Leaflet Draw:区分多个控件

时间:2016-10-14 13:34:38

标签: leaflet

我正在添加两个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事件,并根据我是否已激活drawControldrawControl2来执行不同的操作:

map.on('draw:drawvertex', function (e) {
    console.log("Vertex drawn", e);
});

如何区分哪个drawControl当前处于活动状态?

1 个答案:

答案 0 :(得分:1)

这是一种了解哪种drawControl处于活动状态的脏方法。

诀窍是将它们放在不同的地图角落。它有助于检查用户绘制时可见的ul.leaflet-draw-actionsdiv.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 !');
    }
});

很脏但是有效。