THIS SDK ISDEPRECATED.

We rolled out a new and better SDK for you.

Traffic along the route

Drivers aim to find the fastest route based on the current traffic situation in the area they are in. The Maps SDK allows you to show a layer holding all current traffic jams, visualized by lines in different colors to indicate the severity of the jam. This is updated every minute with very latest traffic speed information in real-time data from TomTom Traffic™.

Sample use case: You are a taxi driver navigating through a city. Thanks to traffic jam information for your car, you see a convenient map area showing you where to move.

Sample use case: As a restaurant delivery service you are interested in finding the fastest route based on current traffic jams, so the food you deliver is fresh and warm.

Use the following code snippets to create your traffic style:

1func trafficStyleMapping() -> [TTRouteTrafficStyle] {
2 [UIColor(red: 0.8, green: 0.6, blue: 0, alpha: 1),
3 UIColor(red: 1, green: 1, blue: 0, alpha: 1),
4 UIColor(red: 1, green: 0.6, blue: 0, alpha: 1),
5 UIColor(red: 1, green: 0.2, blue: 0, alpha: 1),
6 UIColor(red: 0.6, green: 0.2, blue: 0, alpha: 1),
7 UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
8 .map { TTMapRouteStyleLayerBuilder()
9 .withColor(sh)
10 .build()
11 }
12 .map { TTRouteTrafficStyle(routeStyle: [sh]) }
13}
1- (NSArray<TTRouteTrafficStyle *> *)trafficStyleMapping {
2 NSMutableArray<TTRouteTrafficStyle *> *styleMapping = [[NSMutableArray<TTRouteTrafficStyle *> alloc] init]
3 TTRouteTrafficStyle *style6 = [[TTRouteTrafficStyle alloc] initWithRouteStyle:[NSArray arrayWithObject:[[[[TTMapRouteStyleLayerBuilder alloc] init] withColor:[[UIColor alloc] initWithRed:0.8 green:0.6 blue:0.0 alpha:1.0]] build]]];
4 [styleMapping addObject:style6];
5 TTRouteTrafficStyle *style5 = [[TTRouteTrafficStyle alloc] initWithRouteStyle:[NSArray arrayWithObject:[[[[TTMapRouteStyleLayerBuilder alloc] init] withColor:[[UIColor alloc] initWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]] build]]];
6 [styleMapping addObject:style5];
7 TTRouteTrafficStyle *style4 = [[TTRouteTrafficStyle alloc] initWithRouteStyle:[NSArray arrayWithObject:[[[[TTMapRouteStyleLayerBuilder alloc] init] withColor:[[UIColor alloc] initWithRed:1.0 green:0.6 blue:0.0 alpha:1.0]] build]]];
8 [styleMapping addObject:style4];
9 TTRouteTrafficStyle *style3 = [[TTRouteTrafficStyle alloc] initWithRouteStyle:[NSArray arrayWithObject:[[[[TTMapRouteStyleLayerBuilder alloc] init] withColor:[[UIColor alloc] initWithRed:1.0 green:0.2 blue:0.0 alpha:1.0]] build]]];
10 [styleMapping addObject:style3];
11 TTRouteTrafficStyle *style2 = [[TTRouteTrafficStyle alloc] initWithRouteStyle:[NSArray arrayWithObject:[[[[TTMapRouteStyleLayerBuilder alloc] init] withColor:[[UIColor alloc] initWithRed:0.6 green:0.2 blue:0.0 alpha:1.0]] build]]];
12 [styleMapping addObject:style2];
13 TTRouteTrafficStyle *style1 = [[TTRouteTrafficStyle alloc] initWithRouteStyle:[NSArray arrayWithObject:[[[[TTMapRouteStyleLayerBuilder alloc] init] withColor:[[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]] build]]];
14 [styleMapping addObject:style1];
15 return styleMapping;
16}

Use the following code snippets to map your style to magnitude the data from traffic service:

1let styleMapping = trafficStyleMapping()
2var styling = [TTRouteTrafficStyle: [TTTrafficData]]()
3plannedRoute.sections.forEach { section in
4 let density = section.magnitudeOfDelayValue >= 0 ? section.magnitudeOfDelayValue : 5
5 let style = styleMapping[density]
6 var array = styling[style] ?? array.append(TTTrafficData(startPointIndex: section.startPointIndexValue, andEndPointIndex: section.endPointIndexValue))
7 styling[style] = array
8}
1NSArray<TTRouteTrafficStyle *> *trafficStyleMapping = [self trafficStyleMapping];
2NSMutableDictionary<TTRouteTrafficStyle *, NSMutableArray<TTTrafficData *> *> *styling = [@{} mutableCopy];
3NSArray<TTRouteSection *> *sections = plannedRoute.sections;
4for (TTRouteSection *section in sections) {
5 NSInteger density = section.magnitudeOfDelayValue >= 0 ? section.magnitudeOfDelayValue : 5;
6 TTRouteTrafficStyle *style = trafficStyleMapping[density];
7 NSMutableArray<TTTrafficData *> *array = styling[style];
8 if (array == NULL) {
9 array = [@[] mutableCopy];
10 }
11 [array addObject:[[TTTrafficData alloc] initWithStartPointIndex:section.startPointIndexValue andEndPointIndex:section.endPointIndexValue]];
12 styling[style] = array;
13}

Use the following code snippets to display traffic on the route:

mapView.routeManager.showTraffic(on: mapRoute, withStyling: styling)
[self.mapView.routeManager showTrafficOnRoute:mapRoute withStyling:styling];

Screen shots presenting how traffic along the route works:

image

image