Initializing and configuring TomTom Maps and Navigation SDK
TomTomSdk serves as the single entry point to the Navigation SDK. Its purpose is to simplify usage by centralizing initialization and providing consistent, straightforward access to core navigation features—such as navigation, search, reverse geocoding, and route planning—with minimal setup.
Before starting with the Navigation SDK initialization make sure to configure the project as described in the Project setup guide .
The following example shows how to initialize TomTomSdk with the minimum required configuration:
val sdkConfiguration = buildSdkConfiguration( context = context, // Use application context apiKey = BuildConfig.TOMTOM_API_KEY,)
TomTomSdk.initialize( context = context, // Application context (recommended) sdkConfiguration = sdkConfiguration, // See section: Creating SdkConfiguration for different modes)Creating SdkConfiguration
SdkConfiguration defines the parameters you need to initialize the Navigation SDK. Example:
val onlineOnly = buildSdkConfiguration( context = context, // Use application context apiKey = BuildConfig.TOMTOM_API_KEY, coreConfiguration = { telemetryUserConsent = suspend { provideTelemetryConsent() } // See section: Telemetry user consent },)Telemetry user consent
Telemetry consent is required to collect anonymous usage data that helps improve the SDK’s performance, stability, and features. It ensures compliance with privacy regulations and transparency for users. The consent can be set to one of three values: On, Off, or Location Only.
- The initializer takes:
telemetryUserConsent: suspend () → UserConsent, which defines how the SDK retrieves the user’s telemetry consent asynchronously. - You must obtain consent from the user via your app’s UX (e.g., a dialog or onboarding screen) and return the result from this suspend lambda.
- Typical flow:
- On first app start, show a consent dialog.
- Persist the choice (e.g., in
DataStore/SharedPreferences). - In the initializer lambda, return the stored
UserConsent(or collect it on first run).
Example consent provider:
suspend fun provideTelemetryConsent(): UserConsent { // Load saved choice or ask the user via your UI flow. // Return the corresponding UserConsent value. return loadConsentFromStorage() // Your implementation}How to initialize safely the Navigation SDK
There are several ways to safely initialize the Navigation SDK: including directly in the MainActivity, using a ViewModel, or leveraging external dependency injection libraries. Regardless of the chosen approach, ensure that the initialize() method is:
- Called only once.
- Executed off the main thread.
- Invoked after telemetry consent has been obtained.
Below is an example of initialization performed directly in the onCreate method of the MainActivity class:
lifecycleScope.launch { if (!TomTomSdk.isInitialized) { val onlineOnly = buildSdkConfiguration( context = application, apiKey = BuildConfig.TOMTOM_API_KEY, coreConfiguration = { telemetryUserConsent = suspend { provideTelemetryConsent() } }, ) TomTomSdk.initialize( context = application, sdkConfiguration = onlineOnly, ) }}This approach should only be applied after telemetry consent has been determined. This solution is robust against configuration changes—whether the device is rotated or the app is moved to the background, the singleton instance persists, and the isInitialized flag prevents reinitialization.
Accessing SDK components after initialization
After TomTomSdk.isInitialized is true, you can access components and create clients:
// Access the location provider for managing device locationval locationProvider = TomTomSdk.locationProvider// Access the navigation object for route managementval navigation = TomTomSdk.navigation// Create a Search client for location search functionalityval search = TomTomSdk.createSearch()// Create a ReverseGeocoder client for address lookupval reverseGeocoder = TomTomSdk.createReverseGeocoder()// Create a RoutePlanner client for calculating routesval routePlanner = TomTomSdk.createRoutePlanner()