Daniel.fshr
Daniel.fshr

Reputation: 85

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?

Upvotes: 1

Views: 3447

Answers (2)

Kartik Malhotra
Kartik Malhotra

Reputation: 257

In your load function add map.addControl above draw.changeMode("custom_mode")

Upvotes: 2

www-data
www-data

Reputation: 244

You need to add your object first

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

Upvotes: 4

Related Questions