Search code examples
androidfiltersdkmapboxmarkers

How to filter markers in Mapbox Sdk


I'm creating an Android app using MapBox. I've already set up a simple map functionality with markers sourced from .json file. Next step is filtering the markers on the map, just like in this gl-js example here: https://docs.mapbox.com/mapbox-gl-js/example/filter-markers/

I can't find any sdk examples anywhere, and since this is my first app I really can't figure it out on my own. Any help will be appreciated.


Solution

  • You can check out this example https://docs.mapbox.com/android/maps/examples/multiple-expressions-temperature-change/ that features two layers that fetch a min or max temperature from the data source and display it.

    The filtering part is done here:

          // Only display Maximum Temperature in this layer
          maxTempLayer.setFilter(eq(get("element"), literal("All-Time Maximum Temperature")));
          loadedMapStyle.addLayer(maxTempLayer);
    

    Filters accept expressions as arguments, and here Expression.eq is used to compare "element" from the GeoJSON data source (referenced with the Expression.get) with the "All-Time Maximum Temperature" value. If it resolves to true, the feature is going to be displayed, otherwise, it's going to be hidden.