Mapbox-GL setStyle removes layers
Other answers suggest that map.on('styledata')
is the correct approach. But it doesn't fire reliably, and the related isStyleLoaded
is inaccurate.
Looks like we're stuck using style.load
for now.
Here's an example demonstrating that: http://bl.ocks.org/tristen/0c0ed34e210a04e89984
Unlike a mapping library like Leaftlet, Mapbox GL JS doesn't have a concept of "basemap" vs "other layers." All layers are part of the same entity: the style. So you need to keep some state of the data layer around and call its source/addLayer on each change.
Maybe I am late, but just for the record:
style.load
is not part of the public API- use
map.on('styledata')
to monitor the style change
Please reference:
- https://github.com/mapbox/mapbox-gl-js/issues/6210
- https://github.com/mapbox/mapbox-gl-js/issues/3970#issuecomment-275722197