When programmatically changing a map style with map.setStyle(), all its datas are removed. Example in video: http://streamable.com/3i0w
It's not documented but I guess it's not the supposed behavior.
How are you adding the data to the map - by addSource and addLayer? If so, those API calls are adding properties to the style. So, setting the style replaces it, including the things you added: there is no concept of overlay or 'added data' in mapbox gl js, it is all one map.
Yes I'm using addSource and addLayer. Ok I understand, so maybe it should be clarified in the docs? I guess I'm not the only who has been be surprised :) Thanks for your fast answer.
Do you have an idea of how this could be clarified? I think our docs are pretty explicit about the actual behavior of the library, and the expectation that setStyle won't affect a modified style (or that addSource modifies something other than the style) is a misapplied assumption from other mapping libraries.
Yeah Mapbox docs are pretty clear indeed, but despite having spent some hours reading it again and again, this behavior looked like a bug to me :) So I think what you just explained could be added here in the docs: https://www.mapbox.com/mapbox-gl-js/api/#Map.setStyle
setStyle (style)
Replaces the map's style object. Remember: addSource and addLayer API calls are adding properties to the style. So, setting the style replaces it, including the things you added.
I don't think that this is a good addition to documentation. The same is true of setFilter calls, and everything else that explicitly says that it changes styles in the documentation. While it's unfortunate that this cost you some time, our documentation only has space to say what the behavior is, not the many things it isn't.
Most helpful comment
Yeah Mapbox docs are pretty clear indeed, but despite having spent some hours reading it again and again, this behavior looked like a bug to me :) So I think what you just explained could be added here in the docs: https://www.mapbox.com/mapbox-gl-js/api/#Map.setStyle