silicakes
silicakes

Reputation: 6902

Is there a non iterative way to get a computed mapbox-gl layer?

I'm trying to get a specific existing layer, in order to apply some of its paint/layout properties on layer I'm creating.

Currently, I achieve it using:

let remoteLayer: mapboxgl.Layer = this.map.getStyle()
                                  .layers
                                  .find(layer => layer.id == mapboxRemoteLayerID);

Looking at #getLayer's implementation:

getLayer(id: string): Object {
        return this._layers[id];
    }

Which is faster, but returns a layer with precomputed values as they're possiblyEvaluated.

getStyles() is quite expensive as each call runs a serialization method on both layout and paint properties of each layer in the style.

I'd like to either get a single layer computed and serialized, or a dictionary with a (O)1 access time.

Is it possible?

Upvotes: 2

Views: 166

Answers (1)

silicakes
silicakes

Reputation: 6902

After some digging around their code, I've found that this is possible

let remoteLayer: mapboxgl.Layer = (this.map.getLayer(mapboxRemoteLayerID) as any).serialize();

The serialize() method will only serialize the specific layer rather than all others.

I'm not sure if that's supported in their documentation or not, so pay attention when you use it.

Upvotes: 1

Related Questions