Documentation
Initialization
To perform a query to traffic service:
- 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:
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:
An example implementation of the listener to the response from traffic incident details service:
1private val trafficVisitor = object : TrafficIncidentVisitor {23 override fun visit(cluster: TrafficIncidentCluster) {4 listOfIncidents.add(trafficIncidentItemCreator.createClusterOfIncidents(cluster))5 }67 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.