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

Documentation

Documentation

Initialization

To perform a query to traffic service:

  1. Create an 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);
private val trafficApi = OnlineTrafficApi.create(context)

 

The Android Traffic API provides simple yet very handy functionality of
obtaining the traffic data. Application developer can use standard listener approach or use Reactive approach.

First approach involves using the void findFlowSegmentData() or void findIncidentDetails() method, taking
FlowSegmentDataQuery / IncidentDetailsQuery and FlowSegmentDataResultListener / IncidentDetailsResultListener implementation as parameters.
This works analogically for both traffic services. An example query and corresponding listener for 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 of 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 for 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 Maps SDK for Android at
the current version and an internal error is reported.

You are here