Search code examples
javascriptmapboxdrawmapbox-gl

Mapbox Draw (Custom Mode) - Why is changeMode() on draw undefined?


I want to use a custom mode in Mapbox Draw. I declare the draw object like this:

  var draw = new MapboxDraw({
        displayControlsDefault: false,
        controls: {
            polygon: false,
            line_string: true,
            point: false,
            trash: true,
        },
        modes: Object.assign({
            custom_mode: CustomMode,
        }, MapboxDraw.modes),
    });

Then I want to change the mode:

map.on('load', function () {
    draw.changeMode("custom_mode");
    map.addControl(draw, 'bottom-right');
});

Result: Error: Cannot read property 'changeMode' of undefined. But I can log the draw object just before the changeMode() call. What is the problem here?


Solution

  • You need to add your object first

    map.addControl(draw, 'top-left')