0
votes

I have several mapbox datasets, each with a collection of features located in different cities. Each feature has a field named city that contains the city the feature is located in (e.g. city = "new york"). In mapbox studio I have a style with layers made from each of those datasets. I want to load a map with it's bounding box containing all features with city == "new york"(fit map view to features bbox). How can I accomplish that, preferably without ever loading features that don't satisfy city=="new york"

2

2 Answers

1
votes

If I understand you correctly, you have a basemap that contains all the cities, but when you load the map in mapbox-gl-js, you want to filter them to just one particular city.

You do this by calling:

map.setFilter('my-cities-layer', ['==', ['get', 'city'], 'new york']);

There isn't really a simple way of filtering all layers to only include data with that tag, though.

1
votes

After selecting the relevant layer in Mapbox Studio, select the property whose visibility you would like to control. For example, suppose that you are interested in showing a text field for each of these features in your dataset (the same logic could also be applied to icon images). Once this property of the layer is selected, click on the "Style with data conditions" option in the layer editor. You will then be prompted to choose a data field (which in your case will be city) and subsequently prompted to set values for this data field (which in your case will be new york).