Data-driven styling

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.

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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Adding a vector source layer
VectorSource vectorSource = new VectorSource("vector-source", "mapbox://mapbox.mapbox-terrain-v2");
style.addSource(vectorSource);
}
});

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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Adding a raster source layer
RasterSource rasterSource = new RasterSource("raster-source", "mapbox://mapbox.u8yyzaor");
style.addSource(rasterSource);
}
});

GeoJson

Adding a GeoJsonSource can be done 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.

Add a GeoJSON source from a URL:

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
try {
URI geoJsonUrl = new URI("https://url-to-geojson-file.geojson");
GeoJsonSource geoJsonSource = new GeoJsonSource("geojson-source", geoJsonUrl);
style.addSource(geoJsonSource);
} catch (URISyntaxException exception) {
Log.d(TAG, exception);
}
}
});

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

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
try {
GeoJsonSource source = new GeoJsonSource("geojson-source", new URI("asset://local_file_name.geojson"));
style.addSource(source);
} catch (URISyntaxException exception) {
Log.d(TAG, exception);
}
}
});

Create a GeoJSON FeatureCollection and add it to your map:

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Create a list to store our line coordinates.
List routeCoordinates = new 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.
LineString lineString = LineString.fromLngLats(routeCoordinates);
FeatureCollection featureCollection = FeatureCollection.fromFeature(Feature.fromGeometry(lineString));
GeoJsonSource geoJsonSource = new GeoJsonSource("geojson-source", featureCollection);
style.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 shown 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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Set the latitude and longitude coordinates of the image's four corners
LatLngQuad quad = new LatLngQuad(
new LatLng(46.437, -80.425),
new LatLng(46.437, -71.516),
new LatLng(37.936, -71.516),
new LatLng(37.936, -80.425));
// Add the source to the map layer
style.addSource(new ImageSource(ID_IMAGE_SOURCE_ID, quad, DRAWABLE_IMAGE));
// Add layer
RasterLayer layer = new RasterLayer(ID_IMAGE_LAYER, IMAGE_SOURCE_ID);
style.addLayer(layer);
}
});

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

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
ImageSource imageSource = style.getSourceAs(ID_IMAGE_SOURCE);
if (imageSource != null) {
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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
CustomGeometrySource customSource = new CustomGeometrySource(CUSTOM_SOURCE_ID,
GeometryTileProvider);
if (customSource != null){
style.addSource(customSource);
}
}
});

One example of CustomGeometrySource usage is to create a black grid on top of the map. This example's code can be found 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.

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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
RasterDemSource rasterDemSource = new RasterDemSource("source-id", "mapbox://mapbox.terrain-rgb");
style.addSource(rasterDemSource);
// Create hillshade layer source to map
HillshadeLayer hillshadeLayer = new HillshadeLayer("hillshade-layer-id", "source-id").withProperties(
hillshadeHighlightColor(Color.parseColor(HILLSHADE_HIGHLIGHT_COLOR)),
hillshadeShadowColor(Color.BLACK)
);
// Add hillshade layer to map
style.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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
BackgroundLayer backgroundLayer = new BackgroundLayer("background-layer");
backgroundLayer.setProperties(PropertyFactory.backgroundColor(Color.BLUE));
// Add background layer to map
style.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 some vertices and sides might be cut off by the tile boundaries.

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setProperties(PropertyFactory.fillColor(Color.GREEN));
// Add fill layer to map
style.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

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 properties can be set.

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
LineLayer lineLayer = new 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(new Float[]{0.01f, 2f}),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineWidth(5f),
PropertyFactory.lineColor(Color.parseColor("#e55e5e"))
);
style.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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.my_marker_icon);
style.addImage("my-marker-image", icon);
SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(PropertyFactory.iconImage("my-marker-image");
style.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.

Note

Use PropertyFactory.iconOffset if you're using a marker icon as a SymbolLayer icon. The middle of any SymbolLayer icon is placed at the coordinate, rather than the bottom of the marker icon. PropertyFactory.iconOffset will move the marker icon up a certain distance so that the bottom of the marker icon (the pin point) will be on the coordinate location. You'll need to find the correct float value to pass through PropertyFactory.iconOffset, because it will depend on the size of the SymbolLayer icon.

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
SymbolLayer symbolLayer = new SymbolLayer("SYMBOL_LAYER_ID", "SYMBOL_LAYER_SOURCE_ID");
symbolLayer.setProperties(
iconImage("SYMBOL_LAYER_ICON_ID"),
iconOffset(new Float[] {0f, -8f})
);
// Add symbol layer to map
style.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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
RasterSource rasterSource = new RasterSource("source-id", "mapbox://mapbox.u8yyzaor");
style.addSource(rasterSource);
RasterLayer rasterLayer = new RasterLayer("layer-id", "source-id");
style.addLayer(rasterLayer);
}
});

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

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Adding a raster source layer
RasterSource satelliteRasterSource = new RasterSource("satellite-raster-source", "mapbox://mapbox.satellite",512);
style.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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
VectorSource vectorSource = new VectorSource("source-id", "mapbox://mapbox.2opop9hr");
style.addSource(vectorSource);
CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
circleLayer.setSourceLayer("museum-cusco");
circleLayer.setProperties(
PropertyFactory.visibility(Property.VISIBLE),
PropertyFactory.circleRadius(8f),
PropertyFactory.circleColor(Color.argb(1, 55, 148, 179))
);
style.addLayer(circleLayer);
}
});

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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
style.removeLayer("layer-id");
}
});

Removing a source:

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
style.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(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
FillLayer fillLayer = style.getLayerAs("fill-layer-id");
if (fillLayer != null) {
fillLayer.setProperties(PropertyFactory.fillColor(Color.GREEN));
}
}
});

In a GeoJSON source, you are able to change, add, remove, or replace the FeatureCollection like so:

mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
GeoJsonSource geoJsonSource = style.getSourceAs("geojson-source-id");
if (geoJsonSource != null) {
geoJsonSource.setGeoJson(featureCollection);
}
}
});

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.

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

Was this page helpful?