Skip to main content

Draw Shapes

Drawing requires defining sources and layers when your style has loaded, before adding Feature data to them.


Set up sources and layers​

// In MainActivity.kt after style loads
lateinit var pointSource: GeoJsonSource
lateinit var lineSource: GeoJsonSource
lateinit var polygonSource: GeoJsonSource

fun setupLayers(style: Style) {
// For Point
pointSource = GeoJsonSource("point-source")
style.addSource(pointSource)
style.addLayer(SymbolLayer("point-layer", "point-source").withProperties(
iconImage("your-marker-id") // Make sure the icon is included in your style or added manually
))

// For Line
lineSource = GeoJsonSource("line-source")
style.addSource(lineSource)
style.addLayer(LineLayer("line-layer", "line-source").withProperties(
lineColor("#ff0000"),
lineWidth(4f)
))

// For Polygon
polygonSource = GeoJsonSource("polygon-source")
style.addSource(polygonSource)
style.addLayer(FillLayer("polygon-layer", "polygon-source").withProperties(
fillColor("#00ff00"),
fillOpacity(0.5f),
fillOutlineColor("#ff0000")
))
}

Draw a point​

fun drawPoint(latLng: LatLng) {
val point = Point.fromLngLat(latLng.longitude, latLng.latitude)
pointSource.setGeoJson(Feature.fromGeometry(point))
}

A point drawn on the map with the Android Map SDK


Draw a line​

fun drawLine(linePoints: List<Point>) {
val lineString = LineString.fromLngLats(linePoints)
lineSource.setGeoJson(Feature.fromGeometry(lineString))
}

A line drawn on the map with the Android Map SDK


Draw a polygon​

fun drawPolygon(polygon: List<Point>) {
val closed = polygon.toMutableList()
closed.add(polygon.first()) // Ensure the polygon is closed
val pol = Polygon.fromLngLats(listOf(closed))
polygonSource.setGeoJson(Feature.fromGeometry(pol))
}

See also: Android Map SDK overview.