Location tracking

The Mapbox Navigation SDK for iOS enables your application to observe and respond to the user's location. It helps you request permission to access a user's location, use a location provider to get location information for the device, and display the user's location on the map visually.

Privacy and permissions

Users must grant your application permission before it can access information about their location. During this permission prompt, a custom string may be presented explaining how location will be used. This is specified by adding the key NSLocationWhenInUseUsageDescription to the Info.plist file with a value that describes why the application is requesting these permissions.

Prior to iOS 14, the device could only send the user’s exact location. With iOS 14, users can opt into Approximate Location. Since users may toggle precise location off when initial permission for their location is requested by the app or in the System settings, we strongly encourage you to support Approximate Location.

Request temporary access to precise location

Certain application features may require precise location. The Mapbox Navigation SDK for iOS provides a wrapper of Apple’s Core Location APIs that requests temporary access to precise location when the user has opted out at the application settings level:

navigationLocationProvider.requestTemporaryFullAccuracyAuthorization(withPurposeKey: "customKey")

Make the following adjustments to your Info.plist file to provide explanations for system prompts that may appear during location prompts:

  • Provide users with a brief explanation of how the app will use their location data for temporary access:
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Your precise location is used to calculate turn-by-turn directions, show your location on the map, and help improve the map.</string>
    
  • Add LocationAccuracyAuthorizationDescription as an element of the NSLocationTemporaryUsageDescriptionDictionary dictionary to give users a brief explanation of why a feature in your app requires their exact location:
    <key>NSLocationTemporaryUsageDescriptionDictionary</key>
    <dict>
      <key>LocationAccuracyAuthorizationDescription</key>
      <string>Please enable precise location. Turn-by-turn directions only work when precise location data is available.</string>
    </dict>
    

Customize accuracy authorization handling

If you want to customize how accuracy authorization updates are handled, you can use navigationServiceDidChangeAuthorization.

extension ViewController: NavigationServiceDelegate {
    
    public func navigationServiceDidChangeAuthorization(_ service: NavigationService,
                                                        didChangeAuthorizationFor locationManager: CLLocationManager) {
        if #available(iOS 14.0, *),
           locationManager.accuracyAuthorization == .reducedAccuracy {
            navigationMapView?.reducedAccuracyActivatedMode = true
            // Perform other actions in response to the new change in accuracy
        } else {
            navigationMapView?.reducedAccuracyActivatedMode = false
        }
    }
}
Location tracking with the Maps SDK

If you are using the Maps SDK via the Navigation SDK, you have access to additional location tracking capabilities including a protocol for handling changes in location authorization, customzing accuracy authorization handling, and custom location providers. Read more in the Maps SDK's User location guide.