Add a marker to the map
Use MapView
's AnnotationOrchestrator
class to create a PointAnnotationManager
. Create instances of PointAnnotation
and add them to the PointAnnotationManager
using the var annotations: [PointAnnotation]
property.
This example requires an additional asset. Click the button below to download the asset, then add it to your Xcode project's asset catalog and specify it by name as the point annotation's image
.
There are several ways to add markers, annotations, and other shapes to the map using the Maps SDK. To choose the best approach for your application, read the Markers and annotations guide.
ViewController
import UIKitimport MapboxMaps class ViewController: UIViewController { var mapView: MapView! override func viewDidLoad() {super.viewDidLoad() // Center the map camera over Copenhagen.let centerCoordinate = CLLocationCoordinate2D(latitude: 55.665957, longitude: 12.550343)let options = MapInitOptions(cameraOptions: CameraOptions(center: centerCoordinate, zoom: 8.0)) mapView = MapView(frame: view.bounds, mapInitOptions: options)mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]view.addSubview(mapView) // We want to display the annotation at the center of the map's current viewportlet centerCoordinate = mapView.cameraState.center // Make a `PointAnnotationManager` which will be responsible for managing a// collection of `PointAnnotation`s.let pointAnnotationManager = mapView.annotations.makePointAnnotationManager() // Initialize a point annotation with a single coordinate// and configure it with a custom image (sourced from the asset catalogue)var customPointAnnotation = PointAnnotation(coordinate: centerCoordinate) // Make the annotation show a red pincustomPointAnnotation.image = .init(image: UIImage(named: "red_pin")!, name: "red_pin") // Add the annotation to the manager in order to render it on the map.pointAnnotationManager.annotations = [customPointAnnotation]}}