I am using mapbox draw to draw a polygon. I would like it so that there can only be one drawn polygon on the screen at once. For example, the user draws a polygon and then clicks the polygon draw button again, the first polygon would be deleted. I have tried to do this using the draw.modechange but there is an error somewhere in way it is coded. When I clicked the polygon draw button, the existing polygon is deleted but when I try and draw the new polygon nothing is there.
this.map.on('draw.modechange', (e) => {
const data = draw.getAll();
console.log(draw.getMode());
if (draw.getMode() == 'draw_polygon') {
if (data.features.length > 1) {
draw.deleteAll();
}
}
});