Search code examples
mapboxmapbox-gl-jsmapbox-gl

Efficiant loading via filter with Mapbox


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"


Solution

  • 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.