[GIS] Mapbox GL Js: what is the sourceID

javascriptmapboxmapbox-gl-jsmapbox-studio

The documentation for Map.querySourceFeatures says it takes a parameter for sourceID – the ID of the vector tile or GeoJSON source. In their example, they add a source manually:

map.addSource('counties', {
    "type": "vector",
    "url": "mapbox://mapbox.82pkq93d"
});

And so the sourceID is counties.

If I create a Mapbox style in Mapbox Studio and publish it with a url, say:

mapbox://styles/mycoolcompany/mycoolmap123

and pass that directly to the Map constructor like this:

this.map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mycoolcompany/mycoolmap123',
    center: [-123.12, 49.28],
    zoom: 5,
});

How can I get (or set?) the styleID programatically? I'm trying different combinations of things from Mapbox Studio but querySourceFeatures returns an empty array every time with no exceptions or indication of what it's missing.

More succinctly: how do I use querySourceFeatures on a home-made Mapbox Studio Style?

(mapbox-gl 0.36.0)

Best Answer

There isn't a public API to get all source IDs from a map, but

this.map.style.sourceCaches

will return an object wherein each key is a source ID and its value is a source cache (the class responsible for managing an individual source).

Note that Mapbox Studio automatically composites sources — so, if you've added a custom vector source, rather than having multiple vector sources you'll likely see just one (composite), and if you were to download the JSON stylesheet its URL would be something like

"url": "mapbox://mapbox.mapbox-streets-v7,mycoolcompany.randomsha"

so you'll probably be looking for a source called composite.

Related Question