wandergis
wandergis

Reputation: 424

Can we have a feature that can get source's meta info to know the layer id and layer type when add a unknown vector tile services?

Can we have a feature that can get source's meta info to list layers's id and layer's type when add a unknown vector tile service? I think these can help us to style vector service in right way,and can made a default style for different layers (point--circle,polygon--fill,line--line).

Upvotes: 2

Views: 544

Answers (2)

Lucas Wojciechowski
Lucas Wojciechowski

Reputation: 3802

Unfortunately there is no standard way to list all the layers in a vector tile source. Mapbox sources provide a vector_layers object in their TileJSON but because this feature is not standard, it is not available within the GL JS API.

Upvotes: 1

kmandov
kmandov

Reputation: 3209

You can call map.getStyle().layers to find the layers defined in the current style.

Here is how you can get the id and the type of each layer:

var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/light-v9',
    zoom: 13,
    center: [-122.447303, 37.753574]
});

map.on('load', function () {    
    var layersMeta = map.getStyle().layers
        .map(function(layer) { 
            return [layer.id, layer.type];
        });
   console.log(layersMeta);   
});

And here's the corresponding jsfiddle. Have a look at the console.

Upvotes: 2

Related Questions