Mapbox-gl-js: How to get a list of sources?

Created on 1 Nov 2018  路  5Comments  路  Source: mapbox/mapbox-gl-js

mapbox-gl-js version: 0.48

How do I get a list of sources for my map?

Noob question here. How do I get a list of sources for my map? It looks like there is a github issue for mapbox native, but I don't see a corresponding one for mapbox gl js https://github.com/mapbox/mapbox-gl-native/issues/7285

map.getSource('state_label');
gives me undefined.

Most helpful comment

console.log(map.style.sourceCaches); gives you list of sources

All 5 comments

Ah I see this now from #2625

map.querySourceFeatures('composite', {sourceLayer: 'source-name', filter: [filters]})

However, in this case I actually want to modify the state_label data source so I can use 2 letter abbreviations rather than the shortened name. Will I need to get the style update the data there and add it using setStyle or is there an easier way to achieve this?

I can see the layers by logging the composite source, but have no idea how to add the metadata I want to the state_label. And I'm not understanding the difference between 'source' and 'sourceLayer' on the state label layer.

      console.log(this.map.getSource('composite'));
      console.log(this.map.getLayer('state label'));

console.log(map.style.sourceCaches); gives you list of sources

Difference btw source and source-layer you can read here: https://github.com/mapbox/mapbox-gl-js/issues/5820#issuecomment-349775810

You can call map.getStyle().sources to see a list of all sources in your map's style. The same works with layers. Note that you'll have to wait for the style to fully load to do this (e.g. inside a map.on('load', ...) event listener).

Please note that this issue tracker is for reporting bugs and requesting features. For general how-to questions, you can contact our support team or check out sites like Stack Overflow.

Was this page helpful?
0 / 5 - 0 ratings