Search code examples
androidlabelmapboxmapbox-marker

Android Mapbox Marker Labels


I'm trying to figure out how to make a dynamically generated list of markers display a dynamic text label on the map (not in the infoWindow). I know it's possible because I've used this feature in Mapbox Studio - I just can't figure out how to do it on Android! I considered creating a static Bitmap icon with the text incorporated, but that doesn't seem very flexible compared to the Mapbox Studio version, so I'd like to avoid it if I can! Any suggestions?


Solution

  • You're trying to do what's called data-driven styling

    You should use a SymbolLayer to show text on a map. If the text you want to show is based on GeoJSON Features in your data set, then use the get expression in the textField property of a SymbolLayer.

    FeatureCollection featureCollection = FeatureCollection.fromFeatures();
    GeoJsonSource geoJsonSource = new GeoJsonSource("source-id", featureCollection);
    mapboxMap.addSource(geoJsonSource);
    
    SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id")
      .withProperties(PropertyFactory.textField(Expression.get("FEATURE-PROPERTY-KEY")));
    mapboxMap.addLayer(symbolLayer);
    

    The Annotation Plugin simplifies some of this. Also for reference to see how SymbolLayers are used, there is the Mapbox Android demo app.