Create your own provider
Within the TomTom SDKs, developers are not limited to the provided location providers. They can also use their own implementations of the LocationProvider
protocol. This means developers can use their own services to record locations, or develop their own custom simulated location updates. Do not forget to add the specific Location module. See more information in the Quickstart.
Each predefined location provider implements the LocationProvider
protocol and is used in the same way. This guide shows how to use the LocationProvider
. To see how to use it with the Map Display module go to Map User Location. The provider is also used by the Navigation module to configure navigation.
-
Initialize one of the Built-in Location Providers or your own custom implementation.
-
Set the location provider to either the map or navigation.
1let customLocationProvider = DefaultCLLocationProvider()2let configuration = OnlineTomTomNavigationFactory.Configuration(3 navigationTileStore: try NavigationTileStore(config: NavigationTileStoreConfiguration(apiKey: "YOUR_TOMTOM_API_KEY")),4 locationProvider: customLocationProvider,5 routePlanner: routePlanner6)7let navigation = try OnlineTomTomNavigationFactory.create(configuration: configuration) -
Send authorization request by calling
requestWhenInUseAuthorization()
orrequestAlwaysAuthorization()
on theCLLocationManager
instance respectively. Complete instructions to request location permissions on iOS can be found at Requesting authorization for location services.let locationManager = CLLocationManager()locationManager.requestWhenInUseAuthorization() -
Enable the provider to receive location updates.
locationProvider.enable() -
At any time the
LocationProvider
can be disabled to stop receiving location updates.locationProvider.disable() -
Location updates can be observed using the
LocationUpdateObserver
protocol.1func didUpdateLocation(location: GeoLocation) {2 /* YOUR CODE GOES HERE */3}Extend your class with this protocol and add it to the
LocationProvider
instance observers. Remember to remove the observer if it is no longer needed.locationProvider.addObserver(self)locationProvider.removeObserver(self) -
LocationProvider
has a property with the last knownGeoLocation
.let lastLocation = locationProvider.lastKnownLocation