question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamically remove controls with setDrawingOptions

See original GitHub issue

Hi,

I’m changing the options set to drawControl dynamically and I noticed that passing polyline: false for instance, won’t remove/hide the polyline control. If it’s normal, it should be in the doc imo.

Thanks.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
max-checommented, Sep 8, 2015

Just noticed: if after

drawControl.setDrawingOptions({
    polyline: false
});

i do:

map.removeControl(drawControl);

and:

map.addControl(drawControl);

Changes take effect.

1reaction
cavasinfcommented, Oct 18, 2021

Here is what I’ve done to allow the user to only create one shape.

DemoControlsLeaflet

Custom it as you want.


    const map = new L.Map("my_map");
    const drawnItems = new L.FeatureGroup();

    /** ================ CONTROLS PANEL ================ */

    let canCreateNewDraw = true;
    let drawControl;

    _initToolbar();

    function _initToolbar() {
        if (drawControl) map.removeControl(drawControl);

        drawControl = new L.Control.Draw({
            position: 'topleft',
            draw: {
                polyline: false,
                polygon: canCreateNewDraw,
                rectangle: canCreateNewDraw,
                circle: false,
                marker: false,
                circlemarker: false,
            },
            edit: {
                featureGroup: drawnItems,
                remove: true
            }
        });

        map.addControl(drawControl);
    }
    /** ================================================ */

    /** ================ EVENTS ================ */
    map.on(L.Draw.Event.CREATED, function () {
        drawnItems.addLayer(layer);
        canCreateNewDraw = false;
        _initToolbar();
    });

    map.on(L.Draw.Event.DELETED, function () {
        if (drawnItems.getLayers().length === 0) {
            canCreateNewDraw = true;
            _initToolbar();
        }
    });
    /** ======================================== */

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Dynamically remove controls with setDrawingOptions -
Hi,. I'm changing the options set to drawControl dynamically and I noticed that passing polyline: false for instance, won't remove/hide the polyline control...
Read more >
setDrawingOptions after baselayer change Leaflet Draw
I suppose that drawControl is created like: var drawControl = new L.Control.Draw();. If yes, then it is not a property of the map...
Read more >
Leaflet Draw Documentation
The following disables drawing polygons and markers. It also turns off the ability to edit layers. var drawControl = new L.Control.Draw({ draw: {...
Read more >
[Solved] Removing dynamic controls - CodeProject
I think it's time to introduce you to a new idea. Why not treat your three controls as a single control? There is...
Read more >
leaflet-draw | Yarn - Package Manager
... by using the setDrawingOptions method on the Leaflet.draw control. ... #95; Fixed issue where removing the draw control from the map would...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found