THIS SDK ISDEPRECATED.

We rolled out a new and better SDK for you.

Documentation

Initialization

To perform a query to traffic service:

  1. Create a TrafficApi (sdk-traffic) object

You will need the appropriate Traffic API Key. For more information on obtaining and configuring TomTom API Keys and their configuration, go to the "Getting started" subpage.

A TrafficApi object is created in the following manner:

this.trafficApi = OnlineTrafficApi.create(context, BuildConfig.TRAFFIC_API_KEY)
private val trafficApi = OnlineTrafficApi.create(context, BuildConfig.TRAFFIC_API_KEY)

The Android Traffic API provides a simple yet very handy functionality to obtain the traffic data. An application developer can use a standard listener approach or use a Reactive approach.

The first approach involves using the void findFlowSegmentData() or void findIncidentDetails() method, taking the FlowSegmentDataQuery / IncidentDetailsQuery and FlowSegmentDataResultListener / IncidentDetailsResultListener implementation as parameters. This works analogically for both traffic services. An example query and corresponding listener for the Traffic Incident Details service is as follows:

1return IncidentDetailsQueryBuilder.create(IncidentStyle.S1, LONDON_BOUNDING_BOX, DEFAULT_ZOOM_LEVEL_FOR_EXAMPLE, "-1")
2 .withExpandCluster(true)
3 .build();
1IncidentDetailsQueryBuilder.create(IncidentStyle.S1, LONDON_BOUNDING_BOX, DEFAULT_ZOOM_LEVEL_FOR_EXAMPLE, TRAFFIC_MODEL_ID)
2 .withExpandCluster(true)
3 .build()

An example implementation of the listener to the response from traffic incident details service:

1private IncidentDetailsResultListener incidentDetailsResultListener = new IncidentDetailsResultListener() {
2 @Override
3 public void onTrafficIncidentDetailsResult(IncidentDetailsResponse result) {
4
5 final List<TrafficIncidentItem> items = new ArrayList<>();
6
7 TrafficIncidentVisitor visitor = new TrafficIncidentVisitor() {
8 @Override
9 public void visit(TrafficIncidentCluster cluster) {
10 items.add(incidentItemCreator.createClusterOfIncidents(cluster));
11 }
12
13 @Override
14 public void visit(TrafficIncident incident) {
15 items.add(incidentItemCreator.createSingleIncident(incident));
16 }
17 };
18
19 for (BaseTrafficIncident incident : result.getIncidents()) {
20 incident.accept(visitor);
21 }
22
23 view.updateTrafficIncidentsList(items);
24 }
25
26 @Override
27 public void onTrafficIncidentDetailsError(Throwable error) {
28 Toast.makeText(view.getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
29 }
30};
1private val incidentDetailsResultListener = object : IncidentDetailsResultListener {
2 override fun onTrafficIncidentDetailsResult(result: IncidentDetailsResponse) {
3 listOfIncidents.clear()
4
5 context?.let { ctx ->
6 trafficIncidentItemCreator = TrafficIncidentItemCreator(ctx)
7
8 result.incidents.forEach { incident ->
9 incident.accept(trafficVisitor)
10 }
11
12 trafficIncidentAdapter.updateData(listOfIncidents)
13 }
14 }
15
16 override fun onTrafficIncidentDetailsError(error: Throwable) {
17 Toast.makeText(view?.context, error.message, Toast.LENGTH_SHORT).show()
18 }
19}
1private val trafficVisitor = object : TrafficIncidentVisitor {
2
3 override fun visit(cluster: TrafficIncidentCluster) {
4 listOfIncidents.add(trafficIncidentItemCreator.createClusterOfIncidents(cluster))
5 }
6
7 override fun visit(incident: TrafficIncident) {
8 listOfIncidents.add(trafficIncidentItemCreator.createSingleIncident(incident))
9 }
10}

Another way to subscribe a search response is to use a reactive search method. It returns a RxJavas Single<IncidentDetailsResponse> or Single<FlowSegmentDataResponse>. To take advantage of this implementation, one has to subscribe to it (for instance with a Disposable<IncidentDetailsResponse> / Disposable<FlowSegmentDataResponse> or a Consumer<IncidentDetailsResponse> / Consumer<FlowSegmentDataResponse>).

Please note, a consecutive search request cannot be made before its successor’s callback (either onTrafficIncidentDetailsResult or onTrafficIncidentDetailsError) is received.

Concurrent requests to service are not handled in the current version of the Maps SDK for Android, and an internal error is reported.