Tracking / Follow me
Track a vehicle or other assets on the map.
import UIKit
import TrimbleMaps
import TrimbleMapsAccounts
import RoutePlugin
import TrimbleMapsWebservicesClient
class TrackingViewController: UIViewController, AccountManagerDelegate {
internal var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
let account = Account(apiKey: apiKey, region: Region.northAmerica)
AccountManager.default.account = account
AccountManager.default.delegate = self
}
func stateChanged(newStatus: AccountManagerState) {
if newStatus == .loaded {
DispatchQueue.main.async {
// Create a map view
self.mapView = MGLMapView(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
}
}
}
func mapView(_ mapView: MGLMapView, didUpdate userLocation: MGLUserLocation?) {
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>