Skip to content

Instantly share code, notes, and snippets.

@yuletide
Last active February 23, 2023 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuletide/979e0dd8528b5407c8040c6aea3a320b to your computer and use it in GitHub Desktop.
Save yuletide/979e0dd8528b5407c8040c6aea3a320b to your computer and use it in GitHub Desktop.
Get a list of all mapbox layer ids grouped by feature component for easy toggling of mapbox streets layers
#!/usr/bin/env node
// usage node getStyleComponents.js > groups.json
// looks for a style named style.json, rename to match or use process.argv if you want
// then save these ids to an array and toggle with:
// layers?.forEach(lyr => map.setLayoutProperty(lyr, "visibility", value));
const style = require("./style.json");
const components = style.layers.reduce((map, lyr) => {
const component = lyr.metadata?.["mapbox:featureComponent"];
if (map[component]) {
map[component].push(lyr.id);
} else {
map[component] = [lyr.id];
}
return map;
}, {});
console.log(components);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment