All docsMaps SDK for iOS v6ExamplesDisplay a user's approximate location

Display a user's approximate location

A newer version of the Maps SDK is available

This page uses v6.4.1 of the Mapbox Maps SDK. A newer version of the SDK is available. Learn about the latest version, v10.1.0, in the Maps SDK documentation.

Displaying approximate user location is only available on iOS 14+. For more information about requesting location data from users, see Apple's location services guide.

import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
var mapView: MGLMapView?
var preciseButton: UIButton?
override func viewDidLoad() {
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.delegate = self
mapView.showsUserLocation = true
self.mapView = mapView
In order to enable the alert that requests temporary precise location,
please add the following key to your info.plist
You must then add
as a key in the Privacy - Location Temporary Usage Description Dictionary
@available(iOS 14, *)
func mapView(_ mapView: MGLMapView, didChangeLocationManagerAuthorization manager: MGLLocationManager) {
guard let accuracySetting = manager.accuracyAuthorization?() else { return }
if accuracySetting == .reducedAccuracy {
} else {
@available(iOS 14, *)
func addPreciseButton() {
let preciseButton = UIButton(frame:
preciseButton.setTitle("Turn Precise On", for: .normal)
preciseButton.backgroundColor = .gray
preciseButton.addTarget(self, action: #selector(requestTemporaryAuth), for: .touchDown)
self.preciseButton = preciseButton
// constraints
preciseButton.translatesAutoresizingMaskIntoConstraints = false
preciseButton.widthAnchor.constraint(equalToConstant: 150.0).isActive = true
preciseButton.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
preciseButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 100.0).isActive = true
preciseButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
@available(iOS 14, *)
@objc private func requestTemporaryAuth() {
guard let mapView = self.mapView else { return }
let purposeKey = "MGLAccuracyAuthorizationDescription"
mapView.locationManager.requestTemporaryFullAccuracyAuthorization!(withPurposeKey: purposeKey)
private func removePreciseButton() {
guard let button = self.preciseButton else { return }
self.preciseButton = nil