The type currently returned by Map#getLayer is private and has no documented methods. The only documented thing you can do with it is check if it's null or not. This isn't very useful. It would be more useful to return the _serialized_, style spec representation of the layer.
However, this implies that Map#getSource should also return the serialized representation, rather than the internal object. In this case though, we do document things such as GeoJSONSource#setData, which has no Map-level equivalent.
We need to commit one way or the other with these APIs. Mixing and matching behavior is confusing.
Do we really need to expose map.getLayer, or should we just replace it with map.hasLayer?
Do we really need to expose map.getLayer, or should we just replace it with map.hasLayer?
One use case for a serialized map.getLayer is to create a Layer in Studio and save it in your Mapbox Style, then combine it with a local GeoJSON source in GL JS for the actual data.
Update: The current workaround for this is map.getStyle().layers and find the layer you want.
I was talking to the support about this issue exactly. Thanks for the workaround @andrewharvey !
We need to commit one way or the other with these APIs. Mixing and matching behavior is confusing.
How about:
Map#getLayer returns the serialized representation of the layer.Map#getSource returns an object whose _own_ properties constitute the serialized representation of the source, but which may be an instance of a class providing methods that manipulate the internal source.@anandthakker I think that proposal is what I naively assumed was the case: that you can treat the return of either getLayer() or getSource() as an entry in a style's layers or sources object/array, but you can also call the documented methods setData() etc.
If that's hard for some reason, separate methods, perhaps getStyleLayer() and getStyleSource() which return just the serialised versions would be handy.
In general I still find it bit weird and inconsistent that you manipulate layers through a large number of methods on map, but you manipulate sources by calling map.getSource() then calling methods on that object.
Most helpful comment
One use case for a serialized map.getLayer is to create a Layer in Studio and save it in your Mapbox Style, then combine it with a local GeoJSON source in GL JS for the actual data.
Update: The current workaround for this is map.getStyle().layers and find the layer you want.