View annotations
Add view annotations on top of the map view using the Mapbox Maps SDK's View Annotations API. View annotations are UIKit views that the SDK draws on top of a Mapbox MapView
and can be bound to a Geometry
or to a feature rendered on a Layer
. This can be helpful for achieving common patterns used in maps in mobile applications such as showing an information window when tapping a point of interest (POI), or providing estimated time of arrival (ETA) for navigation purposes along a route.
Currently the following Layer types are supported:-
LineLayer
-
FillLayer
-
FillExtrusionLayer
-
CircleLayer
-
SymbolLayer
Benefits:
- Straight-forward API that allows adding an iOS
UIView
as a view annotation, making it possible to add clickable buttons or any other UI elements. - High rendering performance when using a reasonable number of views (generally below 100, but may vary based on the view's content and the user's device model).
- Wide number of options including allowing overlap between view annotations, selected state, connecting to a map feature, and more.
Limitations:
- Inefficient and poor performance when adding many features (> 250) to the map if allow overlap is turned on.
Create a view annotation
Create a UIView
Start by creating a new UIView
or subclass of UIView
that contains the contents of the view annotation. This can include anything from text to images to interactive elements and more.
For example, to create a minimal UIView
containing text:
private func createSampleView(withText text: String) -> UILabel {
let label = UILabel()
label.text = text
label.font = .systemFont(ofSize: 14)
label.numberOfLines = 0
label.textColor = .black
label.backgroundColor = .white
label.textAlignment = .center
return label
}