Skip to content Skip to sidebar Skip to footer

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

Solution 1:

I solved it myself. I'm adding this draw control when map initialized.

drawControl=newL.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.

functionsetDrawingTools(layerType) {

    map.removeControl(drawControl);if(layerType=='Polygon') {

        drawControl=newL.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
        });
    } elseif(layerType=='Line') {

        drawControl=newL.Control.Draw({
            draw : {
                position :'topleft',
                polygon :false,
                polyline : {
                    metric :false
                },
                rectangle :false,
                circle :false,
                marker :false
            },
            edit :false
        });
    } elseif(layerType=='Point') {

        drawControl=newL.Control.Draw({
            draw : {
                position :'topleft',
                polygon :false,
                polyline :false,
                rectangle :false,
                circle :false

            },
            edit :false
        });

    }
    map.addControl(drawControl);
}

Solution 2:

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.

Solution 3:

It sounds like you are trying to gain better control of you layers. Difficult to say without any code posted. Have you considered adding and removing your layers upon user selection? Here is some documentation on manipulating layers. And the syntax would be something like:

map.addLayer(layer_in_question);

or

map.removeLayer(layer_in_question);

Post a Comment for "Leaflet Draw Plugin: How To Hide/show Drawing Tools By Layer Type Dynamically"