【问题标题】:Leaflet Draw: differentiating between multiple controlsLeaflet Draw:区分多个控件
【发布时间】:2017-04-01 02:59:44
【问题描述】:

我正在像这样添加两个 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?

【问题讨论】:

    标签: leaflet


    【解决方案1】:

    这是知道哪个drawControl处于活动状态的一种肮脏方式。

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

    它很脏,但它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      相关资源
      最近更新 更多