Traffic along the route


Can someone please help me with an example on how to show traffic on route using JAVA. I get that i need a list of colours that are converted to RouteLayerStyle and Traffic Data. But how do i get route.sections or magnitudeOfDelay?

Update and Finish: I have found what i was looking for. This is my Java example if someone needs it:
Yet another update: i found the magnitude of delay api reference so code needs to be updated even on the main portal cos DELAY_MAGNITUDE_UNKNOWN = 5 is actually 0;
The magnitude of delay associated with an incident. The values meaning:

  • 0: Unknown

  • 1: Minor

  • 2: Moderate

  • 3: Major

  • 4: Undefined (used for road closures and other indefinite delays)

          RoutingApi routingApi = OnlineRoutingApi.create(activity, "your Api key");
          RouteDescriptor routeDescriptor = new RouteDescriptor.Builder()
          RouteCalculationDescriptor routeCalculationDescriptor = new RouteCalculationDescriptor.Builder()
          RouteSpecification routeSpecification = new RouteSpecification.Builder(startLatlng, endLatlng)
          RouteCallback routeCallback = new RouteCallback() {
              public void onSuccess(@NonNull RoutePlan routePlan) {
                  List<FullRoute> list = routePlan.getRoutes();
                  if (!routePlan.getRoutes().isEmpty()) {
                      FullRoute fullRoute = list.get(0);
                      RouteBuilder routeBuilder = new RouteBuilder(fullRoute.getCoordinates()).style(routeStyle);
                      route = tomMap.addRoute(routeBuilder);
                      int[] Colors = new int[]{Color.TRANSPARENT, Color.parseColor("#FDC530"), Color.parseColor("#F9B023"), Color.parseColor("#DF1B12"), Color.parseColor("#B1110E")};
                      List<Integer> densityList = new ArrayList<>();
                      Map<RouteLayerStyle, List<TrafficData>> trafficStyleMap = new HashMap<>();
                      fullRoute.getSections().forEach((section) -> {
                          int DELAY_MAGNITUDE_UNKNOWN = 0;
                          int density;
                          if (section.getMagnitudeOfDelay() >= 0){
                              density = section.getMagnitudeOfDelay();
                          }else {
                              density = DELAY_MAGNITUDE_UNKNOWN;
                          RouteLayerStyle routeLayerStyleSection = new RouteLayerStyle.Builder().color(Colors[density]).build();
                          TrafficData trafficDataSection = new TrafficData(section.getStartPointIndex(), section.getEndPointIndex());
                          trafficStyleMap.put(routeLayerStyleSection, Collections.singletonList(trafficDataSection));
                      tomMap.getRouteSettings().showTrafficOnRoute(route.getId(), trafficStyleMap);
                      int maxDensity = Collections.max(densityList);
                      if (maxDensity == 0){
                           if (darkMode){
                              }else {
                      }else {
              public void onError(@NonNull RoutingException e) {
                  Log.e("routing ", "error: " + e.getLocalizedMessage());
          routingApi.planRoute(routeSpecification, routeCallback);
1 Like