Skip to content

Add a GeoJSON Layer

Add a GeoJsonSource to the style, then draw it with a CircleLayer. This is the same source-and-layer model used by MapLibre on the web.

import org.maplibre.android.style.sources.GeoJsonSource
import org.maplibre.android.style.layers.CircleLayer
import org.maplibre.android.style.layers.PropertyFactory
import org.maplibre.geojson.Feature
import org.maplibre.geojson.FeatureCollection
import org.maplibre.geojson.Point

mapView.getMapAsync { map ->
    map.getStyle { style ->
        style.addSource(
            GeoJsonSource(
                "points",
                FeatureCollection.fromFeatures(
                    arrayOf(Feature.fromGeometry(Point.fromLngLat(16.37, 48.21)))
                )
            )
        )
        style.addLayer(
            CircleLayer("points-layer", "points").withProperties(
                PropertyFactory.circleRadius(8f),
                PropertyFactory.circleColor(android.graphics.Color.parseColor("#e8551a")),
                PropertyFactory.circleStrokeWidth(2f),
                PropertyFactory.circleStrokeColor(android.graphics.Color.WHITE)
            )
        )
    }
}