Skip to content

Building Layer

Note

You can find the full source code of this example in BuildingFillExtrusionActivity.kt of the MapLibreAndroidTestApp.

In this example will show how to add a Fill Extrusion layer to a style.

Map data OpenStreetMap. © OpenMapTiles.

We use the OpenFreeMap Bright style which, unlike OpenFreeMap Libery, does not have a fill extrusion layer by default. However, if you inspect this style with Maputnik you will find that the multipolygons in the building layer (of the openfreemap source) each have render_min_height and render_height properties.

Setting up the fill extrusion layer
val fillExtrusionLayer = FillExtrusionLayer("building-3d", "openmaptiles")
fillExtrusionLayer.sourceLayer = "building"
fillExtrusionLayer.setFilter(
    Expression.all(
        Expression.has("render_height"),
        Expression.has("render_min_height")
    )
)
fillExtrusionLayer.minZoom = 15f
fillExtrusionLayer.setProperties(
    PropertyFactory.fillExtrusionColor(Color.LTGRAY),
    PropertyFactory.fillExtrusionHeight(Expression.get("render_height")),
    PropertyFactory.fillExtrusionBase(Expression.get("render_min_height")),
    PropertyFactory.fillExtrusionOpacity(0.9f)
)
style.addLayer(fillExtrusionLayer)
Changing the light direction
isInitPosition = !isInitPosition
if (isInitPosition) {
    light!!.position = Position(1.5f, 90f, 80f)
} else {
    light!!.position = Position(1.15f, 210f, 30f)
}
Changing the light color
isRedColor = !isRedColor
light!!.setColor(ColorUtils.colorToRgbaString(if (isRedColor) Color.RED else Color.BLUE))