0

I add a draw-toolbar (I'm using the my own version from the example "Disabling a toolbar item")

drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var drawControl = new L.Control.Draw({
    draw: {
        polygon: false,
        marker: false
    },
    edit: {
        featureGroup: drawnItems 
    }
});
map.addControl(drawControl);

This works great but I can't remove the created toolbar with all the handlers. How can I achieve this?

I tried:

map.removeControl(drawControl);             //dosent remove handlers
drawControl.remove();                       //dosent remove handlers
drawControl.removeToolbar();                //dosent find function
drawControl._toolbars.edit.removeToolbar(); //dosent find function
drawControl._toolbars.draw.removeToolbar(); //dosent find function
drawControl._toolbars.removeToolbar();      //dosent find function

Edit for better understanding of my problem

The documentation claims that I can add a toolbar like these:

var toolbar = L.Toolbar();
toolbar.addToolbar(map);

How ever this gives me a this.callInitHooks is not a function TypeError and it's not what i need because I want to customize the buttons depending on what the user can edit.

So I go with the drawControl version for the toolbar. But for this verion there is no function like removeToolbar().

The only thing I coud find was the first two of my "I tried" exampels. But this only remove the html container and not the class with all the handels like the removeToolbar() function. (At least thats was I think it does when I look at the code from the removeToolbar() function ).

Redraw

var drawControl = "";

function addToolbar() {
    if( drawControl !== "" ) {
        /* remove old toolbar */
        map.removeControl(drawControl);             //dosent remove handlers
        drawControl.remove();                       //dosent remove handlers
        drawControl.removeToolbar();                //dosent find function
        drawControl._toolbars.edit.removeToolbar(); //dosent find function
        drawControl._toolbars.draw.removeToolbar(); //dosent find function
        drawControl._toolbars.removeToolbar();      //dosent find function
    }

    /* draws new toolbar (with different drawnItems and e.g. marker: true)*/
    drawnItems = new L.FeatureGroup();
    map.addLayer(drawnItems);
    drawControl = new L.Control.Draw({
        draw: {
            polygon: false,
            marker: false
        },
        edit: {
            featureGroup: drawnItems 
        }
    });
    map.addControl(drawControl);

    map.on(L.Draw.Event.CREATED, function( e ) {
        console.log('created');
    });
}

But because it not fully removes the old toolbar. It fires the L.Draw.Event.CREATED also from the old toolbar.

So how can I fully remove the old toolbar?

  • What do you mean exactly by "all the handlers"? – IvanSanchez Jun 4 at 9:20
  • The draw-events are still firing when i redraw the the toolbar with different settings. – Silvan Jun 4 at 9:22
  • What do you mean with "redraw the toolbar with different settings"? Can you please edit the question and exactly describe all the steps that lead to undesired behavior? What are this undesired events end when are they triggered? – TomazicM Jun 4 at 19:01
  • @TomazicM I edited the question. I hope you have now all information you need. – Silvan Jun 5 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.