Sorry, you need to enable JavaScript to visit this website.

Documentation

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:

_

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

 

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

_

private IncidentDetailsResultListener incidentDetailsResultListener = new IncidentDetailsResultListener() {
    @Override
    public void onTrafficIncidentDetailsResult(IncidentDetailsResponse result) {

        final List<TrafficIncidentItem> items = new ArrayList<>();

        TrafficIncidentVisitor visitor = new TrafficIncidentVisitor() {
            @Override
            public void visit(TrafficIncidentCluster cluster) {
                items.add(incidentItemCreator.createClusterOfIncidents(cluster));
            }

            @Override
            public void visit(TrafficIncident incident) {
                items.add(incidentItemCreator.createSingleIncident(incident));
            }
        };

        for (BaseTrafficIncident incident : result.getIncidents()) {
            incident.accept(visitor);
        }

        view.updateTrafficIncidentsList(items);
    }

    @Override
    public void onTrafficIncidentDetailsError(Throwable error) {
        Toast.makeText(view.getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
    }
};
private val incidentDetailsResultListener = object : IncidentDetailsResultListener {
    override fun onTrafficIncidentDetailsResult(result: IncidentDetailsResponse) {
        listOfIncidents.clear()

        context?.let { ctx ->
            trafficIncidentItemCreator = TrafficIncidentItemCreator(ctx)

            result.incidents.forEach { incident ->
                incident.accept(trafficVisitor)
            }

            trafficIncidentAdapter.updateData(listOfIncidents)
        }
    }

    override fun onTrafficIncidentDetailsError(error: Throwable) {
        Toast.makeText(view?.context, error.message, Toast.LENGTH_SHORT).show()
    }
}
private val trafficVisitor = object : TrafficIncidentVisitor {

    override fun visit(cluster: TrafficIncidentCluster) {
        listOfIncidents.add(trafficIncidentItemCreator.createClusterOfIncidents(cluster))
    }

    override fun visit(incident: TrafficIncident) {
        listOfIncidents.add(trafficIncidentItemCreator.createSingleIncident(incident))
    }
}

 

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.