Skip to main content

Data-driven styling

A newer version of the Maps SDK is available
This page uses v9.7.1 of the Mapbox Maps SDK. A newer version of the SDK is available. Learn about the latest version, v11.3.0, in the Maps SDK documentation.

Use the Maps SDK's data-driven styling capabilities to create and display many types of data. You can dynamically change the look and feel of your map in real time based on the information within a particular dataset. Data-driven styling is largely built on the idea of sources and layers.

guide
Mapbox Style Specification

View a full list of the features in runtime styling not specific to Android.

chevron-right
Annotation Plugin for Android
Sources and layers provide nimble options for customizing the look of a Mapbox map and the data displayed on the map. The Mapbox Annotation Plugin for Android provides a simplified system for interacting with and customizing Mapbox map layers.

Sources

Sources hold the actual data and layers reference sources. That is how to show data on your Mapbox map. There are a handful of different source types supported and choosing the correct one to use depends on your data type. Adding a source won't instantly make data appear on the map because sources don't contain styling details like color or width. Layers refer to a source and give it a visual representation.

Two parameters are required to use a source. A source requires a unique String ID and requires some sort of data.

Vector

VectorSource tiles must be in Mapbox Vector Tile format. All layers that use a vector source must specify a "source-layer" value. For vector tiles hosted by Mapbox, the URL value should be of the form mapbox://mapid.

mapboxMap.getStyle {

// Adding a vector source layer
val vectorSource = VectorSource("vector-source", "tileset-id")

it.addSource(vectorSource)
}

When adding a new layer to a map style at runtime using a VectorSource, you must specify a source layer using the setSourceLayer() method (even when a tileset contains only one source layer). To see the available source layers in a tileset, visit your Mapbox account's Tilesets page and click on the tileset to open the tileset information page, which lists all source layers. Identify which source layer's data you want to be attributed to the map's layer.

This setSourceLayer() method is prohibited for all other source types including GeoJSON sources.

mapboxMap.getStyle {

val vectorSource = VectorSource("vector-source", "tileset-id")

it.addSource(vectorSource)

val circleLayer = CircleLayer("circle-layer-id", "vector-source")

circleLayer.setSourceLayer(NAME_OF_LAYER_INSIDE_TILESET)

style.addLayer(circleLayer)
}

Raster

RasterSource tiles can be added to your map if they are in TileJSON format. If hosted by Mapbox, the URL value should be of the form mapbox://mapid.

mapboxMap.getStyle {

// Adding a raster source layer
val rasterSource = RasterSource("raster-source", "mapbox://mapbox.u8yyzaor")

it.addSource(rasterSource)

}

GeoJson

You can add a GeoJsonSource in a few different ways. You can provide a URL to the GeoJSON raw data hosted online, provide a link to a GeoJSON file hosted locally inside of the app's assets folder, or you can build your own GeoJSON FeatureCollection directly inside of the code. The snippets of code below show the different ways to add a GeoJSON source to your map.

example
Add a GeoJSON source

Create a GeoJSON feature collection dynamically and then add it as a map source.

chevron-right

Add a GeoJSON source from a URL:

mapboxMap.getStyle {

try {
val geoJsonUrl = URI("https://url-to-geojson-file.geojson")
val geoJsonSource = GeoJsonSource("geojson-source", geoJsonUrl)
it.addSource(geoJsonSource)
} catch (exception: URISyntaxException) {
Log.d(TAG, exception)
}
}

You can also load a locally stored GeoJSON file by using a URI

mapboxMap.getStyle {

try {

val source = GeoJsonSource("geojson-source", URI("asset://local_file_name.geojson"))

it.addSource(source)

} catch (exception: URISyntaxException) {

Log.d(TAG, exception)
}
}

Create a GeoJSON FeatureCollection and add it to your map:

mapboxMap.getStyle {

// Create a list to store our line coordinates.
val routeCoordinates = ArrayList<Point>()
routeCoordinates.add(Point.fromLngLat(-118.394391, 33.397676))
routeCoordinates.add(Point.fromLngLat(-118.370917, 33.391142))

// Create the LineString from the list of coordinates and then make a GeoJSON FeatureCollection so that you can add the line to our map as a layer.

val lineString = LineString.fromLngLats(routeCoordinates)

val featureCollection = FeatureCollection.fromFeature(Feature.fromGeometry(lineString))

val geoJsonSource = GeoJsonSource("geojson-source", featureCollection)
it.addSource(geoJsonSource)
}

A benefit of having your data inside a GeoJSON source is that you can update, remove, or add additional Features inside the source at any time, providing a solution to animating data in your map through the Runtime Styling API. For example, an Android ValueAnimator can move a feature by updating its coordinates within the GeoJSON data.

Image

ImageSource allows for a georeferenced raster image to be displayed on top of the map. The georeferenced image scales and rotates as the user zooms, tilts, and rotates the map. The geographic location of the raster image content, supplied with LatLngQuad, can be non-axis aligned.

mapboxMap.getStyle {

// Set the latitude and longitude coordinates of the image's four corners
val quad = LatLngQuad(
LatLng(46.437, -80.425),
LatLng(46.437, -71.516),
LatLng(37.936, -71.516),
LatLng(37.936, -80.425))

it.addSource(ImageSource(ID_IMAGE_SOURCE, quad, DRAWABLE_IMAGE_HERE))

// Add layer
val layer = RasterLayer(ID_IMAGE_LAYER, ID_IMAGE_SOURCE)
it.addLayer(layer)
}

The setImage() method is a convenient way to update the ImageSource's image by passing in a drawable.

mapboxMap.getStyle {

val imageSource = style.getSourceAs<ImageSource>(ID_IMAGE_SOURCE)

imageSource?.setImage(DESIRED_IMAGE)}

Custom geometry

A CustomGeometrySource is helpful in situations when you have data which is dynamically generated or needs to be loaded on demand. A FeatureCollection with any type and number of GeoJSON geometries can be used in a CustomGeometrySource.

mapboxMap.getStyle {

val customSource = CustomGeometrySource(CUSTOM_SOURCE_ID, GeometryTileProvider())

it.addSource(customSource)

}

One example of CustomGeometrySource usage is to create a black grid on top of the map. Find the example's code in the GridSourceActivity of the Maps SDK for Android test application.

Raster DEM

RasterDemSource supports Mapbox Terrain RGB (mapbox://mapbox.terrain-rgb) and Mapzen Terrarium tile formats. This source should be used with a Maps SDK HillshadeLayer.

The Mapbox terrain tileset is for adding hill terrain to any Mapbox map. Runtime styling can also be used to change the hillshade appearance.

mapboxMap.getStyle {

val rasterDemSource = RasterDemSource("source-id", "mapbox://mapbox.terrain-rgb")
style.addSource(rasterDemSource)

// Create hillshade layer source to map
val hillshadeLayer = HillshadeLayer("hillshade-layer-id", "source-id").withProperties(
hillshadeHighlightColor(Color.parseColor(HILLSHADE_HIGHLIGHT_COLOR)),
hillshadeShadowColor(Color.BLACK)
)

// Add hillshade layer to map
it.addLayer(hillshadeLayer)
}

Layers

While sources hold the data, layers are used to style and display the information. Several layer types are offered depending on your source geometry. Except for layers of the background type, each layer needs to refer to a source. You can optionally filter features and then define how those features are styled.

Each layer offers a setProperties API which can be used to style the layer in many different ways. Note that instead of creating different layers depending on certain cases inside your source data, it's recommended to use data-driven styling to reduce the number of layers that the map needs to render.

Background

The background layer type is unique in that it doesn't require a source. Background layers can be a solid color or a pattern.

mapboxMap.getStyle {

val backgroundLayer = BackgroundLayer("background-layer")
backgroundLayer.setProperties(PropertyFactory.backgroundColor(Color.BLUE))

// Add background layer to map
it.addLayer(backgroundLayer)
}

Fill

Fill layers have an enclosed shape geometry that can be useful for marking areas on a map. Use a FillLayer with GeoJSON Polygon or MultiPolygon geometries. The geometry is like a line layer consisting of a series of coordinates in a particular order with the first and last points having the same coordinate. The geometry is "enclosed" when the coordinate list starts and ends with the same coordinates. If the geometry isn't enclosed, the FillLayer will render but tile boundaries may cut off some vertices and sides.

mapboxMap.getStyle {

val fillLayer = FillLayer("layer-id", "source-id")
fillLayer.setProperties(PropertyFactory.fillColor(Color.GREEN))

// Add fill layer to map
it.addLayer(fillLayer)
}

To alter the shape of the geometry once you have added it, the layer can stay with no changes needed, only the source it's using should be updated. The layer will always display the latest updates inside its source.

Line

guide
Create and display line layer

Build a GeoJSON FeatureCollection with the line geometry and then display it on the map using a line layer.

chevron-right

A series of coordinates can be combined to create a line segment that shows on a map. Between each pair of coordinates, a line segment's created which gets drawn straight and connects the two points.

Before beginning, you'll want to make sure that the Source your layer will be using has lineStrings as part of its geometry and you can find an example of this in the GeoJSON source section. Once the source has been created and added to the map, a lineLayer can be started, and it's properties declared.

mapboxMap.getStyle {

val lineLayer = LineLayer("line-layer", "line-source")

// The layer properties for our line. This is where you can make the line dotted, set the color, etc.
lineLayer.setProperties(
PropertyFactory.lineDasharray(arrayOf(0.01f, 2f)),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineWidth(5f),
PropertyFactory.lineColor(Color.parseColor("#e55e5e"))
)
it.addLayer(lineLayer)
}

Symbol

Symbol layers show a single location on the map with either an icon or text label. Like GL Markers and Marker Views, the symbol layer can represent the same data and offers the most power for in map displaying. To begin with, you will add a marker image to the map and then display it as a symbol layer.

mapboxMap.getStyle {

val icon = BitmapFactory.decodeResource(resources, R.drawable.my_marker_icon)
it.addImage("icon-id", icon)

val symbolLayer = SymbolLayer("layer-id", "source-id")
symbolLayer.setProperties(PropertyFactory.iconImage("icon-id"))

it.addLayer(symbolLayer)
}

Not only can symbol layers mark locations on the map using an image, but they can also display text directly on the map. The SDK handles text symbol layers in a similar process to the image snippet given above, only the properties of the layer change.

Icon anchors and offsets

By default a SymbolLayer icon is anchored to the coordinate at the icon's center. While this is appropriate for many icons, it may not be appropriate for icons shaped like teardrop markers or pushpins, when you would expect the bottom of the icon to be anchored to the coordinate. In these cases, you can adjust the position of the icon by specifying an anchor or setting an offset.

Default marker icon
If you are using the mapbox_marker_icon_default image included with the Mapbox Maps SDK for Android, you do not need to adjust the icon anchor. The image includes bottom spacing that's equal to half the pin height, so the point will align with the coordinate.

Anchor: Using PropertyFactory.iconAnchor is the recommended way to adjust the icon's location. For example, using PropertyFactory.iconAnchor(Property.ICON_ANCHOR_BOTTOM) tells the Maps SDK to attach the image's bottom to the coordinate.

Offset: PropertyFactory.iconOffset is alternative solution that allows you to specify the distance that you'd like the icon to be moved from the anchor point. For example, using PropertyFactory.iconOffset(new Float[] {0f, VERTICAL_FLOAT_VALUE}), where VERTICAL_FLOAT_VALUE is some distance, tells the Maps SDK to move the icon that distance (up or down) from the coordinate. The appropriate VERTICAL_FLOAT_VALUE will depend on the height of your icon.

mapboxMap.getStyle {

val symbolLayer = SymbolLayer("SYMBOL_LAYER_ID", "SYMBOL_LAYER_SOURCE_ID")

symbolLayer.setProperties(
iconImage("SYMBOL_LAYER_ICON_ID"),
iconAnchor(Property.ICON_ANCHOR_BOTTOM),
// or iconOffset(arrayOf(0f, -9f))
)

it.addLayer(symbolLayer)
}

))

Raster

Raster layers are typically a collection of images that display on top of the base map tiles. While vector tiles are preferred, satellite imagery or legacy map styles render as a raster layer.

mapboxMap.getStyle {

val rasterSource = RasterSource("source-id", "mapbox://mapbox.u8yyzaor")
it.addSource(rasterSource)

val rasterLayer = RasterLayer("layer-id", "source-id")
it.addLayer(rasterLayer)
}

One common use case for a RasterLayer is adding a layer of satellite tiles to the map:

mapboxMap.getStyle {

// Adding a raster source layer
val satelliteRasterSource = RasterSource("satellite-raster-source", "mapbox://mapbox.satellite", 512)

it.addSource(satelliteRasterSource)
}

Circle

Circle layers have a single center coordinate which comes from the source data. It's a geographically accurate projection of a circle on the Earth's surface drawn on the map. A few default properties are provided but can be overridden when the layer's first created.

mapboxMap.getStyle {

val circleLayer = CircleLayer("layer-id", "source-id")

circleLayer.setProperties(
PropertyFactory.circleRadius(FLOAT_OR_EXPRESSION),
PropertyFactory.circleColor(COLOR_OR_EXPRESSION)
)

it.addLayer(circleLayer)
}

Fill extrusion

Fill extrusion layers add 3D polygon "extrusions" to the map. This layer is often used to show 3D buildings, but any type of Polygon shape can be extruded with this layer type. Find more information on styling 3D shape extrusions in the Extrusions guide.

mapboxMap.getStyle {

val fillExtrusionLayer = FillExtrusionLayer("extrusion-layer-id", "source-id")

fillExtrusionLayer.setProperties(
fillExtrusionHeight(HEIGHT_NUMBER)
)

it.addLayer(fillExtrusionLayer)
}

Hillshade

A HillshadeLayer displays elevation data that comes from Mapbox Terrain RGB tile formats. This layer should be used with a RasterDemSource.

Read more information on accessing elevation data in the Access elevation data guide.

The hillshade section of the official Mapbox Style Specification has information on styling the hillshading's shadow color, light direction, and more.

mapboxMap.getStyle {

val rasterDemSource = RasterDemSource("source-id", "mapbox://mapbox.terrain-rgb")
style.addSource(rasterDemSource)

val hillshadeLayer = HillshadeLayer("hillshade-layer-id", "source-id").withProperties(
hillshadeHighlightColor(Color.parseColor(HILLSHADE_HIGHLIGHT_COLOR)),
hillshadeShadowColor(Color.BLACK)
)

it.addLayer(hillshadeLayer)
}
example
Hillshading

Add and style terrain hillshading.

chevron-right

Heatmap

The Maps SDK's HeatmapLayer provides heatmap visualizations and styling options for performant heatmaps. The two main categories for using this layer are:

  • Dense Point data. Heatmaps are a useful way to visualize the density of a particular phenomenon when it would be visually overwhelming to display each point individually.
  • Interpolating discrete values over a continuous surface, creating a smooth gradient between those points. This type of heatmap is less common and does not visualize density by aggregating features within a set of boundaries in the way a choropleth map does, but instead displays a continuous gradient between points. For example, your town may only have a few weather stations, but your favorite weather app displays a smooth gradient of temperatures across the entire area of your town. For your local weather service, it is reasonable to assume that, if two adjacent stations report different temperatures, the temperature between them will transition gradually from one to the next. Read the heatmap section in Mapbox's style specification to learn more about the radius, weight, intensity, color, and opacity options for heatmaps.
mapboxMap.getStyle {

val heatmapLayer = HeatmapLayer("layer-id", "source-id")

heatmapLayer.setProperties(
PropertyFactory.heatmapColor(COLOR_OR_EXPRESSION),
PropertyFactory.heatmapWeight(FLOAT_OR_EXPRESSION)
)

it.addLayer(heatmapLayer)
}
example
Heatmap data

Use a HeatmapLayer to visualize data

chevron-right
example
Heatmap data

Use expressions to style the HeatmapLayer with precise control

chevron-right

Removing sources and layers

A source cannot be removed if it's still used by any layer. The removal will fail and log a console warning. Starting in the 7.0.0 release of the Maps SDK, the SDK changed remove methods to return a boolean which states whether the removal was successful.

All layers using a particular source must be removed before that source can be removed.

Removing a layer:

mapboxMap.getStyle {

it.removeLayer("layer-id")

}

Removing a source:

mapboxMap.getStyle {

it.removeSource("source-id")

}

Modify properties

Sources and layers aren't immutable so they can be modified anytime during the map render. For example, to alter the fill color of a layer after it's been added to the map, you use the map's Style object to get the layer and set the property.

mapboxMap.getStyle {

val fillLayer = style.getLayerAs<FillLayer>("fill-layer-id")

fillLayer?.setProperties(PropertyFactory.fillColor(Color.GREEN))
}

In a GeoJSON source, you are able to change, add, remove, or replace the FeatureCollection. You can pass in a List of Features, a single Feature, or a Geometry:

mapboxMap.getStyle {

val geoJsonSource = style.getSourceAs<GeoJsonSource>("geojson-source-id")

val newFeatureCollectionFromFeatureList: FeatureCollection = FeatureCollection.fromFeatures(FEATURE_LIST)

val newFeature = Feature.fromGeometry(Point.fromLngLat(0.0, 0.0))

val newLineString: LineString = LineString.fromLngLats(POINT_LIST)

geoJsonSource?.setGeoJson(newFeatureCollectionFromFeatureList)
}

Capturing click events

Layers are not clickable and don't expose any event listeners for you to handle user input. Instead, the map querying tools can help you detect when a user has interacted with the map. For example, when a FillLayer's Polygon has been tapped on.

Or, you can use the Mapbox Annotation Plugin, which provides onClick() and onLongClick() listening.