Starting navigation

The Navigation SDK for iOS is only available upon request. Contact Sales to get started.

This guide explains how to start turn-by-turn navigation and retrieve route progress information, such as the remaining travel time and traveled distance. For this example, we utilize an online route.

Project setup

Configure the project according to the project setup guide and import the necessary frameworks using the following instructions, based on your preferred package manager:

Swift Package Manager
  1. Open your App’s target and navigate to General > Frameworks, Libraries, and Embedded Content.
  2. Add the following TomTomSDK libraries from the provided code snippet. Once the project is set up, import the mentioned frameworks into your code.
import CoreLocation
import TomTomSDKCommon
import TomTomSDKLocationProvider
import TomTomSDKNavigation
import TomTomSDKNavigationEngines
import TomTomSDKNavigationOnline
import TomTomSDKNavigationTileStore
import TomTomSDKRoute
import TomTomSDKRoutePlanner
import TomTomSDKRoutePlannerOnline
import TomTomSDKRoutingCommon
CocoaPods
  1. Open your project’s Podfile and add the required modules to your application’s target:
    TOMTOM_SDK_VERSION = '0.71.1'
    target 'YourAppTarget' do
    use_frameworks!
    pod 'TomTomSDKCommon', TOMTOM_SDK_VERSION
    pod 'TomTomSDKLocationProvider', TOMTOM_SDK_VERSION
    pod 'TomTomSDKNavigation', TOMTOM_SDK_VERSION
    pod 'TomTomSDKNavigationEngines', TOMTOM_SDK_VERSION
    pod 'TomTomSDKRoute', TOMTOM_SDK_VERSION
    pod 'TomTomSDKRoutePlanner', TOMTOM_SDK_VERSION
    pod 'TomTomSDKRoutePlannerOnline', TOMTOM_SDK_VERSION
    pod 'TomTomSDKRouteReplannerDefault', TOMTOM_SDK_VERSION
    end
  2. Install the dependencies by executing the following commands in the project directory:
    pod repo update tomtom-sdk-cocoapods
    pod install --repo-update
  3. Once the project is set up and the navigation module is added to your target, import the following frameworks:
    import CoreLocation
    import TomTomSDKCommon
    import TomTomSDKLocationProvider
    import TomTomSDKNavigation
    import TomTomSDKNavigationEngines
    import TomTomSDKNavigationOnline
    import TomTomSDKNavigationTileStore
    import TomTomSDKRoute
    import TomTomSDKRoutePlanner
    import TomTomSDKRoutePlannerOnline
    import TomTomSDKRoutingCommon

Starting navigation

Start by initializing a RoutePlanner and a TomTomNavigation object to build an example of navigating a route.

To make it easier to follow this guide, make sure to place all the following code snippets in the above-mentioned class.

class NavigationExample {
private let routePlanner = OnlineRoutePlanner(apiKey: "YOUR_TOMTOM_API_KEY")
private var navigation: TomTomNavigation!
}

Before starting navigation with a route, you need to plan the route and set up a location provider to receive location updates along the route.

First, plan a Route between two locations. Refer to the Planning a route guide if you want to learn more about how to plan a Route .

typealias PlanRouteResult = Result<(Route?, RoutePlanningOptions), Error>
private func planRoute(completionHandler: @escaping (PlanRouteResult) -> ()) {
do {
// Create itinerary.
let amsterdam = ItineraryPoint(
coordinate: CLLocationCoordinate2D(
latitude: 52.37616,
longitude: 4.90828
)
)
let rotterdam = ItineraryPoint(
coordinate: CLLocationCoordinate2D(
latitude: 51.90546,
longitude: 4.46662
)
)
let itinerary = Itinerary(origin: amsterdam, destination: rotterdam)
// Create RoutePlanningOptions based on itinerary.
let routePlanningOptions = try RoutePlanningOptions(
itinerary: itinerary,
guidanceOptions: GuidanceOptions()
)
// Request a route based on the previously created options.
routePlanner.planRoute(
options: routePlanningOptions,
onRouteReady: nil,
completion: { result in
switch result {
case let .success(routePlanningResponse):
let result = (
routePlanningResponse.routes?.first,
routePlanningOptions
)
completionHandler(.success(result))
case let .failure(error):
print(error.localizedDescription)
completionHandler(.failure(error))
}
}
)
} catch {
completionHandler(.failure(error))
}
}

Once you have retrieved the route, create and enable the LocationProvider . Refer to the Built-in location providers and the Location module guides for more information.

let simulatedLocationProvider = SimulatedLocationProvider(
delay: Measurement<UnitDuration>(value: 0.5, unit: .seconds),
adjustToCurrentTime: true
)
simulatedLocationProvider.updateCoordinates(
route.geometry,
interpolate: true
)
simulatedLocationProvider.enable()

Now you can set up the navigation with the OnlineTomTomNavigationFactory.Configuration using:

  • Your TomTom API key
  • The previously created location provider
  • A RoutePlanner to be used to update the active route plan or to replan after deviation.
let tileStore: NavigationTileStore
do {
tileStore = try NavigationTileStore(
config: NavigationTileStoreConfiguration(apiKey: "YOUR_TOMTOM_API_KEY")
)
} catch {
print("Cannot create Tile Store: \(error)")
return
}
let navigationConfiguration = OnlineTomTomNavigationFactory
.Configuration(
navigationTileStore: tileStore,
locationProvider: simulatedLocationProvider,
routePlanner: routePlanner
)

Next, create the TomTomNavigation object with the previous configuration.

self.navigation = try! OnlineTomTomNavigationFactory
.create(configuration: navigationConfiguration)

Start receiving route progress updates during navigation by implementing the NavigationProgressObserver protocol and adding it to the initialized TomTomNavigation object.

extension NavigationExample: NavigationProgressObserver {
func didUpdateProgress(progress: TomTomSDKNavigationEngines.RouteProgress) {
/* Your code goes here */
}
}
self.navigation.addProgressObserver(self)

To start navigation along a route, pass the NavigationOptions object which sets the active route to the above-created route.

let routePlan = RoutePlan(
route: route,
routePlanningOptions: routePlanningOptions
)
let navigationOptions = NavigationOptions(activeRoutePlan: routePlan)
do {
try self.navigation.start(navigationOptions: navigationOptions)
} catch {
print("start navigation error: \(error)")
}

Combine all the previous snippets into the following format:

func prepareAndStartNavigation() throws {
planRoute { [weak self] result in
switch result {
case let .success(planRouteResult):
guard let self,
let route: Route = planRouteResult.0
else { return }
let routePlanningOptions: RoutePlanningOptions = planRouteResult.1
// Initialize the simulated location provider and enable it.
let simulatedLocationProvider = SimulatedLocationProvider(
delay: Measurement<UnitDuration>(value: 0.5, unit: .seconds),
adjustToCurrentTime: true
)
simulatedLocationProvider.updateCoordinates(
route.geometry,
interpolate: true
)
simulatedLocationProvider.enable()
// Configure navigation.
let tileStore: NavigationTileStore
do {
tileStore = try NavigationTileStore(
config: NavigationTileStoreConfiguration(apiKey: "YOUR_TOMTOM_API_KEY")
)
} catch {
print("Cannot create Tile Store: \(error)")
return
}
let navigationConfiguration = OnlineTomTomNavigationFactory
.Configuration(
navigationTileStore: tileStore,
locationProvider: simulatedLocationProvider,
routePlanner: routePlanner
)
// Create an instance of navigation
// based on the previous configuration.
self.navigation = try! OnlineTomTomNavigationFactory
.create(configuration: navigationConfiguration)
// Add a progress observer to the navigation instance.
self.navigation.addProgressObserver(self)
// Start navigation on the planned route.
let routePlan = RoutePlan(
route: route,
routePlanningOptions: routePlanningOptions
)
let navigationOptions = NavigationOptions(activeRoutePlan: routePlan)
do {
try self.navigation.start(navigationOptions: navigationOptions)
} catch {
print("start navigation error: \(error)")
}
case let .failure(error):
print("route planning error: \(error.localizedDescription)")
}
}
}

Once you have started the navigation session, TomTomNavigation will:

Retrieving route progress information

Once navigation has started you can observe route progress updates through the NavigationProgressObserver.didUpdateProgress(progress:) callback and retrieve the travelled distance and the remaining travel time. The following snippet prints information to validate the route progress updates.

func didUpdateProgress(progress: TomTomSDKNavigationEngines.RouteProgress) {
print("Distance along the route: \(progress.distanceAlongRoute)")
print("Remaining travel time: \(progress.remainingTime)")
}

In addition to route progress updates, you can also listen to and handle events for route updates, deviations, route and lane guidance, waypoint arrival and reaching a destination. Refer to the Turn-by-turn navigation guide to find out more about supported navigation events.

Next steps

Now that you know how to start navigation with a route and have learnt about navigation events, here are the recommendations on what to explore next: