Mapbox-gl-draw change drawing style after click a button

1.4k views Asked by At

I am trying to let the user choose their paint color and then paint polygon on map. I know there is a setFeatureProperty method, but it needs to pass in a feature ID and thus user will need to draw the shape first and then change style. Is there anyway to just change to draw style programmably for future drawing? Any help is appreciated.

1

There are 1 answers

0
Toni BCN On

Based on the Gist https://gist.github.com/dnseminara/0790e53cef9867e848e716937727ab18

It's possible to get feature ID:

// callback for draw.update and draw.selectionchange
var setDrawFeature = function(e) {
    if (e.features.length && e.features[0].type === 'Feature') {
        var feat = e.features[0];
        drawFeatureID = feat.id;
    }
}

I've made this Jsfidle: https://jsfiddle.net/ToniBCN/y79ajgrw/3/ Maybe it could help you because it's easy to change feature color before draw and not after like the example.