All docsMaps SDK for iOSExamplesDisplay the user's location

Display the user's location

Display the user's location on a map with the default user location puck.

import UIKit
import MapboxMaps
public class ViewController: UIViewController {
internal var mapView: MapView!
internal var cameraLocationConsumer: CameraLocationConsumer!
internal let toggleBearingImageButton: UIButton = UIButton(frame: .zero)
internal var showsBearingImage: Bool = false {
didSet {
override public func viewDidLoad() {
// Set initial camera settings
let options = MapInitOptions(cameraOptions: CameraOptions(zoom: 15.0))
mapView = MapView(frame: view.bounds, mapInitOptions: options)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Setup and create button for toggling show bearing image
cameraLocationConsumer = CameraLocationConsumer(mapView: mapView)
// Add user position icon to the map with location indicator layer
mapView.location.options.puckType = .puck2D()
// Allows the delegate to receive information about map events.
mapView.mapboxMap.onNext(.mapLoaded) { _ in
// Register the location consumer with the map
// Note that the location manager holds weak references to consumers, which should be retained
self.mapView.location.addLocationConsumer(newConsumer: self.cameraLocationConsumer)
self.finish() // Needed for internal testing purposes.
@objc func showHideBearingImage() {
func syncPuckAndButton() {
// Update puck config
let configuration = Puck2DConfiguration.makeDefault(showBearing: showsBearingImage)
mapView.location.options.puckType = .puck2D(configuration)
// Update button title
let title: String = showsBearingImage ? "Hide bearing image" : "Show bearing image"
toggleBearingImageButton.setTitle(title, for: .normal)
private func setupToggleShowBearingImageButton() {
// Styling
toggleBearingImageButton.backgroundColor = .systemBlue
toggleBearingImageButton.addTarget(self, action: #selector(showHideBearingImage), for: .touchUpInside)
toggleBearingImageButton.setTitleColor(.white, for: .normal)
toggleBearingImageButton.translatesAutoresizingMaskIntoConstraints = false
// Constraints
toggleBearingImageButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0).isActive = true
toggleBearingImageButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0).isActive = true
toggleBearingImageButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -100.0).isActive = true
// Create class which conforms to LocationConsumer, update the camera's centerCoordinate when a locationUpdate is received
public class CameraLocationConsumer: LocationConsumer {
weak var mapView: MapView?
init(mapView: MapView) {
self.mapView = mapView
public func locationUpdate(newLocation: Location) {
to: CameraOptions(center: newLocation.coordinate, zoom: 15),
duration: 1.3)