Skip to main content

Annotations

Annotations are images or individually-styled circle, line, or polygon features that are rendered above the map and fixed to specific geographic coordinates.

Looking for a simpler option?

If you need basic pin-style markers in Jetpack Compose, consider using Markers instead. Markers provide a quick way to add default-styled markers without requiring custom image assets.

Benefits:

  • Available in both Jetpack Compose and traditional Android Views.
  • Built-in interaction support like selecting and dragging annotations around the map.
  • No external data file necessary.
  • No specific data format other than geographic coordinates (longitude and latitude).
  • Every annotation can be individually styled.
  • Every annotation layer can be adjusted to be above or below another layer.
  • Same performance benefits as using style layers.

Limitations:

  • No default image available.
  • (use Markers for built-in markers).
  • Inefficient for adding many features (> 250) to the map.

In Android View, add annotations to the map with the MapView’s AnnotationPlugin. Use the AnnotationPlugin to create annotation managers based on the type of annotation that you're interested in. Every annotation manager handles a collection of annotations. Once a manager has been created, you can create and add individually styled instances of the corresponding annotation type.

Point annotations

A PointAnnotation can display an image at any geographic coordinate. A common use case is to display a marker or pin representing a location that a user can tap on to get more information.

PointAnnotation works with any image format supported by Android, including PNG, JPEG, GIF, WebP, and vector drawables.

Bring your own image

The Maps SDK for Android does not provide a default image for point annotations. You must provide your own image asset. If you're using Jetpack Compose and need pin-style markers, consider using Markers instead.

The code snippet below shows how to add a point annotation to the map using an image asset named red_marker. You can download a PDF containing a red map marker to try out the PointAnnotation example.

arrow-downDownload red marker image
// Create an instance of the Annotation API and get the PointAnnotationManager.
val annotationApi = mapView?.annotations
val pointAnnotationManager = annotationApi?.createPointAnnotationManager(mapView)
// Set options for the resulting symbol layer.
val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
// Define a geographic coordinate.
.withPoint(Point.fromLngLat(18.06, 59.31))
// Specify the bitmap you assigned to the point annotation
// The bitmap will be added to map style automatically.
.withIconImage(YOUR_ICON_BITMAP)
// Add the resulting pointAnnotation to the map.
pointAnnotationManager?.create(pointAnnotationOptions)
EXAMPLE
Add a marker to the map

Use the Annotations API to add a single red marker pin to the map using the Maps SDK for Android.

Other shapes

The Annotation API also supports putting other shapes on the map including circles using CircleAnnotationManager, polylines using PolylineAnnotationManager, and polygons using PolygonAnnotationManager. These annotations work like the point annotations described above, but do not require an image. The options available for each type of annotation varies and you can find a full list in the API reference documentation.

Circle annotation

A circle annotation (CircleAnnotation) places a circle at a point on the map.

// Create an instance of the Annotation API and get the CircleAnnotationManager.
val annotationApi = mapView?.annotations
val circleAnnotationManager = annotationApi?.createCircleAnnotationManager(mapView)
// Set options for the resulting circle layer.
val circleAnnotationOptions: CircleAnnotationOptions = CircleAnnotationOptions()
// Define a geographic coordinate.
.withPoint(Point.fromLngLat(18.06, 59.31))
// Style the circle that will be added to the map.
.withCircleRadius(8.0)
.withCircleColor("#ee4e8b")
.withCircleStrokeWidth(2.0)
.withCircleStrokeColor("#ffffff")
// Add the resulting circle to the map.
circleAnnotationManager?.create(circleAnnotationOptions)
EXAMPLE
Add circle annotations

Use the Annotations API to add many colored circles to the map using the Maps SDK for Android.

Polyline annotation

A polyline annotation (PolylineAnnotation) connects a list of coordinates on the map with a polyline. The order of the coordinates in the list will determine the order in which to connect the points, in the same way that coordinates are handled in the GeoJSON specification.

// Create an instance of the Annotation API and get the polyline manager.
val annotationApi = mapView?.annotations
polylineAnnotationManager = annotationApi.createPolylineAnnotationManager(mapView)
// Define a list of geographic coordinates to be connected.
val points = listOf(
Point.fromLngLat(17.94, 59.25),
Point.fromLngLat(18.18, 59.37)
)
// Set options for the resulting line layer.
val polylineAnnotationOptions: PolylineAnnotationOptions = PolylineAnnotationOptions()
.withPoints(points)
// Style the line that will be added to the map.
.withLineColor("#ee4e8b")
.withLineWidth(5.0)
// Add the resulting line to the map.
polylineAnnotationManager?.create(polylineAnnotationOptions)
EXAMPLE
Add polyline annotations

Use the Annotations API to add many polylines to the map using the Maps SDK for Android.

Polygon annotation

A polygon annotation (PolygonAnnotation) takes a list of coordinates and will try to connect those coordinates and add the resulting polygonal shape to the map. The order of the coordinates in the list matters and works the same way as in the GeoJSON specification.

// Create an instance of the Annotation API and get the polygon manager.
val annotationApi = mapView?.annotations
val polygonAnnotationManager = annotationApi?.createPolygonAnnotationManager(mapView)
// Define a list of geographic coordinates to be connected.
val points = listOf(
listOf(
Point.fromLngLat(17.94, 59.25),
Point.fromLngLat(18.18, 59.25),
Point.fromLngLat(18.18, 59.37),
Point.fromLngLat(17.94, 59.37)
)
)
// Set options for the resulting fill layer.
val polygonAnnotationOptions: PolygonAnnotationOptions = PolygonAnnotationOptions()
.withPoints(points)
// Style the polygon that will be added to the map.
.withFillColor("#ee4e8b")
.withFillOpacity(0.4)
// Add the resulting polygon to the map.
polygonAnnotationManager?.create(polygonAnnotationOptions)
EXAMPLE
Add a polygon to the map

Use the Annotations API to add a single red polygon annotation to the map using the Maps SDK for Android.

Interactivity

The Annotation API includes the ability make annotations draggable and handles drag gestures for you. You can specify that annotations should be draggable when adding the annotation to the map initially.

val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
.withPoint(Point.fromLngLat(18.06, 59.31))
.withIconImage(YOUR_ICON_BITMAP)
// Make the annotation draggable.
.withDraggable(true)
// Add the draggable pointAnnotation to the map.
pointAnnotationManager?.create(pointAnnotationOptions)

Or, you can update an annotation that's already been added to the map to be draggable.

// When a user clicks on a draggable menu item defined in the layout,
// then toggle whether all point annotations are draggable.
R.id.menu_action_draggable -> {
pointAnnotationManager?.annotations?.forEach {
it.isDraggable = !it.isDraggable
}
}

Removing Annotations

To completely remove an annotation manager, call annotationApi.removeAnnotationManager(), passing the annotation manager that you want to remove. This removes the backing source and layer. The removed annotation manager will not be useful after it is removed.

Annotation managers are removed implicitly if another annotation manager is created with the same ID.

Was this page helpful?