Traffic support

VERSION 0.64.0
PUBLIC PREVIEW

Offline functionality for the Maps and Navigation SDKs for iOS is only available upon request. Contact us to get started.

This guide explains how to integrate traffic events during offline or non-connected use cases. The traffic module provides events for use cases like offline guidance, route calculation, and map visualization, in the form of traffic incidents. Traffic data can be retrieved near the current location, and optionally along the route.

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

Traffic requires access to the tomtom-sdk-spm-offline package.

Here’s how to add this package to your project:

  1. Click "File" > "Add Packages… / Add Package Dependencies…​" in XCode.
  2. In the "Enter Package URL" textbox, enter the URL for tomtom-sdk-spm-offline package.
  3. Set the "Dependency Rule" to "Exact Version."
  4. Ensure that you’ve selected the appropriate project where you want to add this dependency and click "Add Package".
  5. For test purposes, choose TomTomSDKTraffic from the Offline product list.
  6. Once the package is successfully resolved and added to your project, you can use this package by importing its modules into your Swift files:
    import TomTomSDKTraffic
CocoaPods

Traffic requires access to the TomTomSDKTraffic module.

Here’s how to add them to your project:

  1. Add the TomTomSDKTraffic module to your project’s Podfile.
    1TOMTOM_SDK_VERSION = '0.64.0'
    2
    3target 'YourAppTarget' do
    4 use_frameworks!
    5 pod 'TomTomSDKTraffic', TOMTOM_SDK_VERSION
    6end
  2. Update and install the dependencies by executing the following command in the project directory:
    pod install --repo-update
  3. Once the pod install command is successfully finished, you should have the .xcworkspace file in the project directory. Open that .xcworkspace file to start working on the Xcode project.

For further configuration of the Traffic client, you can explore the API documentation.

Displaying traffic on the map

For offline use cases, you can display traffic incidents on the map as provided by the traffic provider, e.g., in the vicinity of the current location. Traffic information for arbitrary locations and traffic flow are not supported. To display traffic the on map, the traffic data provider must be added when setting the map data provider. The following code snippet demonstrates how to show traffic on the map, while setting the map data provider. This example was created from the Traffic object using the TrafficDataProviderFactory.

1guard let offlineDataProvider: MapDisplayDataProvider = OfflineTileDataProviderFactory
2 .createOfflineTileDataProvider(store: mapDataStore) else {
3 fatalError("Ofline data provider initialization failed.")
4}
5let trafficDataProvider: MapDisplayDataProvider = TrafficDataProviderFactory
6 .createTrafficDataProvider(trafficService: trafficService)
7
8let mapOptions = MapOptions(
9 apiKey: "YOUR_TOMTOM_API_KEY",
10 cacheConfiguration: nil,
11 styleMode: .main,
12 dataProviders: [offlineDataProvider, trafficDataProvider]
13)
14var mapView = MapView(mapOptions: mapOptions)

With this setup, it is possible to enable traffic on the route with a simple statement, as shown in the following code snippet:

map.showTrafficIncidents()

Integrating traffic for route calculation within vicinity

When instantiating the OfflineRoutePlanner, add the reference to the traffic object so the routing client can use traffic information for route planning and route section population. Here is an example on how to achieve this:

1var routePlanner: RoutePlanner!
2do {
3 try routePlanner = OfflineRoutePlanner(
4 store: mapDataStore,
5 trafficService: trafficService
6 )
7} catch {
8 fatalError("Router planer initialization failed.")
9}

Integrating traffic for route calculation beyond vicinity

The TomTom Traffic service supports traffic along planned routes, providing additional traffic information for offline routes. However, enabling traffic information for offline routes may lead to increased data consumption. Bellow a more comprehensive example with integration to the navigation service:

1var ndsMapContext: NDSMapContext!
2do {
3 try ndsMapContext = NDSMapContext(
4 ndsStore: mapDataStore,
5 updater: nil
6 )
7} catch {
8 fatalError("Map context initialization failed.")
9}
10let navigationConfiguration = OfflineTomTomNavigationFactory.Configuration(
11 ndsMapContext: ndsMapContext,
12 locationProvider: locationProvider,
13 routePlanner: routePlanner
14)
15guard let navigation = try? OfflineTomTomNavigationFactory.create(configuration: navigationConfiguration) else {
16 fatalError("The navigation could not be created!")
17}

Visualizing traffic on the route

Traffic information can also be integrated into the offline routing to display traffic incidents along the route to provide the user with a richer experience. The following code snippet shows how to enable traffic visualization in the map:

1var routingVisualization: any RoutingVisualization
2do {
3 let customStyle = try RouteTrafficIncidentStyle()
4 try routingVisualization = RoutingVisualizationFactory.create(
5 map: map,
6 styleConfiguration: StyleConfiguration(routeTrafficIncident: customStyle)
7 )
8 routingVisualization.areTrafficIncidentsEnabled = true
9} catch {
10 fatalError("Routing visualization initialization failed.")
11}

Next steps

Since you have learned how to support traffic on offline maps, here are recommendations for the next steps:

  • Search: Learn more about integrating search features into your map.
  • Reverse geocoding: Learn how to convert geographic coordinates into a human-readable address.

By diving deeper into these areas, you can unlock the full potential of offline maps in your application.