Leaf draw: differentiation between multiple controls

I am adding two Drawlet Draw examples ( https://github.com/Leaflet/Leaflet.draw ) like this (using only strings):

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);

Now I want to listen to the event draw:drawvertexand do different things depending on whether I activated drawControlor drawControl2:

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

How can I tell which drawControl is currently active?

+4
source share
1 answer

Here's a dirty way to find out which drawControl is active.

The trick is to place them in different corners of the map. This helps to check which is ul.leaflet-draw-actionsdisplayed when the user draws. For example, in div.leaflet-topor in 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 !');
    }
});

, .

+1

Source: https://habr.com/ru/post/1657769/


All Articles