Tracking / Follow me
Track a vehicle or other assets on the map.
 
import Foundation
import UIKit
import TrimbleMaps
import TrimbleMapsAccounts
import TrimbleMapsWebservicesClient
class TrackingViewController: UIViewController, AccountManagerDelegate {
    internal var mapView: TMGLMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let apiKey =  "Your-API-key-here"
        let account = Account(apiKey: apiKey, region: Region.northAmerica)
        AccountManager.default.account = account
        AccountManager.default.delegate = self
    }
    func stateChanged(newStatus: AccountManagerState) {
        if AccountManager.default.isLicensed(licensedFeature: .mapsSdk) {
            if newStatus == .loaded {
                DispatchQueue.main.async {
                    // Create a map view
                    self.mapView = TMGLMapView(frame: self.view.bounds)
                    // Add the map
                    self.view.addSubview(self.mapView)
                    // Start tracking the user
                    self.mapView.userTrackingMode = .followWithHeading
                    self.mapView.showsUserLocation = true
                    self.mapView.showsUserHeadingIndicator = true
                }
            }
        } else {
            // Handle the case where the account is not licensed for Maps SDK
            print("Account is not licensed for Maps SDK")
        }
    }
    func mapView(_ mapView: TMGLMapView, didUpdate userLocation: TMGLUserLocation?) {
        guard let coord = userLocation?.coordinate else {
            return
        }
        mapView.setCenter(coord, animated: true)
    }
}
The following entries in the application’s Info.plist are required in order to ask the user for location permissions.
<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>
    <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>