Beta
Maps SDK for iOS v10
All docsMaps SDK for iOS v10ExamplesAdd a default marker to the map

Add a default 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 syncAnnotations(_ annotations: [PointAnnotation]) method. Note that the syncAnnotations(_ annotations: [PointAnnotation]) method allows you to declaratively manage the annotations being shown on the map.

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 UIKit
import MapboxMaps
public class ViewController: UIViewController {
internal var mapView: MapView!
private var pointAnnotationManager: PointAnnotationManager?
override public 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)
// Allows the delegate to receive information about map events.
mapView.mapboxMap.onNext(.mapLoaded) { [weak self] _ in
guard let self = self else { return }
self.setupExample()
}
}
func setupExample() {
// We want to display the annotation at the center of the map's current viewport
let centerCoordinate = mapView.cameraState.center
// Make a `PointAnnotationManager` which will be responsible for managing a
// collection of `PointAnnotion`s.
let pointAnnotationManager = mapView.annotations.makePointAnnotationManager()
// Initialize a point annotation with a geometry ("coordinate" in this case)
// and configure it with a custom image (sourced from the asset catalogue)
var customPointAnnotation = PointAnnotation(coordinate: centerCoordinate)
// Make the annotation show the default red pin
customPointAnnotation.image = .default
// Add the annotation to the manager in order to render it on the mao.
pointAnnotationManager.syncAnnotations([customPointAnnotation])
// The annotations added above will show as long as the `PointAnnotationManager`
// is alive, so keep a reference to it.
self.pointAnnotationManager = pointAnnotationManager
}
}