Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet Draw Plugin: How to hide/show drawing tools by Layer Type dynamically

I'm using draw plugin in my project and I would like to know how can I hide/show drawing tools by layer type?

For example, let's say I have 2 layers one of them type is Polygon and the other one is Line.

If user select Polygon layer, I want to hide Line drawing tool.

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?

Every suggestion will be appreciated.

like image 884
Emre Koç Avatar asked Oct 30 '13 07:10

Emre Koç


2 Answers

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);
}
like image 152
Emre Koç Avatar answered Oct 03 '22 00:10

Emre Koç


It appears you can't do that with the plugin, but you can use CSS to show/hide certain drawing tools when you switch layers.

The buttons have classes like leaflet-draw-draw-polyline, leaflet-draw-draw-polygon, etc.

like image 38
Dan Burzo Avatar answered Oct 03 '22 01:10

Dan Burzo