Add Polyline Annotations
This example demonstrates using PolylineAnnotation
with Jetpack Compose in the Mapbox Maps SDK, adding a polyline on the map with specified points, line color, and line width. When interacting with the polyline annotation, a toast message is displayed on click. Additionally, a PolylineAnnotationGroup
is employed to showcase multiple polyline annotations with randomized points and colors on the map. The AnnotationConfig
class provides configuration options for annotations.
There are several ways to add markers, annotations, and other shapes to the map using the Maps SDK. To choose the appropriate approach for your application, read the Markers and annotations guide.
This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.
See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.
package com.mapbox.maps.compose.testapp.examples.annotation
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.mapbox.geojson.Point
import com.mapbox.maps.compose.testapp.ExampleScaffold
import com.mapbox.maps.compose.testapp.examples.utils.AnnotationUtils
import com.mapbox.maps.compose.testapp.examples.utils.CityLocations
import com.mapbox.maps.compose.testapp.ui.theme.MapboxMapComposeTheme
import com.mapbox.maps.extension.compose.MapboxMap
import com.mapbox.maps.extension.compose.animation.viewport.rememberMapViewportState
import com.mapbox.maps.extension.compose.annotation.generated.PolylineAnnotation
import com.mapbox.maps.extension.compose.annotation.generated.PolylineAnnotationGroup
import com.mapbox.maps.extension.compose.annotation.generated.withLineColor
import com.mapbox.maps.logD
import com.mapbox.maps.plugin.annotation.AnnotationConfig
import com.mapbox.maps.plugin.annotation.generated.PolylineAnnotationOptions
import java.util.Random
/**
* Example to showcase usage of PolylineAnnotation with Jetpack Compose.
*/
public class PolylineAnnotationActivity : ComponentActivity() {
private val random = Random()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MapboxMapComposeTheme {
ExampleScaffold {
val mapViewportState = rememberMapViewportState {
setCameraOptions {
center(CityLocations.NULLISLAND)
zoom(3.0)
pitch(0.0)
bearing(0.0)
}
}
MapboxMap(
Modifier.fillMaxSize(),
mapViewportState = mapViewportState
) {
PolylineAnnotation(
points = POLYLINE_POINTS,
) {
interactionsState.onClicked {
Toast.makeText(
this@PolylineAnnotationActivity,
"Clicked on Polygon Annotation: $it",
Toast.LENGTH_SHORT
).show()
true
}
.onLongClicked {
Toast.makeText(
this@PolylineAnnotationActivity,
"Long Clicked on Polygon Annotation: $it",
Toast.LENGTH_SHORT
).show()
true
}
.onDragged {
logD(
this.javaClass.simpleName,
"Dragging Polyline Annotation: $it"
)
}.also { it.isDraggable = true }
lineColor = Color.Red
lineWidth = 5.0
}
PolylineAnnotationGroup(
annotations = mutableListOf<PolylineAnnotationOptions>().apply {
repeat(100) {
add(
PolylineAnnotationOptions()
.withPoints(AnnotationUtils.createRandomPoints())
.withLineColor(
Color(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256),
)
)
)
}
},
annotationConfig = AnnotationConfig(
PITCH_OUTLINE, LAYER_ID, SOURCE_ID
)
)
}
}
}
}
}
private companion object {
val POLYLINE_POINTS = listOf(
Point.fromLngLat(-4.375974, -2.178992),
Point.fromLngLat(-7.639772, -4.107888),
Point.fromLngLat(-11.439207, 2.798737),
)
private const val LAYER_ID = "line_layer"
private const val SOURCE_ID = "line_source"
private const val PITCH_OUTLINE = "pitch-outline"
}
}