Traffic along the route

Hi,

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()
                  .routeType(RouteType.FASTEST)
                  .considerTraffic(true)
                  .travelMode(TravelMode.CAR)
                  .departAt(TrueTime.now())
                  .build();
          RouteCalculationDescriptor routeCalculationDescriptor = new RouteCalculationDescriptor.Builder()
                  .routeDescription(routeDescriptor)
                  .maxAlternatives(1)
                  .reportType(ReportType.EFFECTIVE_SETTINGS)
                  .instructionType(InstructionsType.TAGGED)
                  .computeTravelForTraffic(TrafficInformation.ALL)
                  .sectionType(SectionType.TRAFFIC)
                  .build();
          RouteSpecification routeSpecification = new RouteSpecification.Builder(startLatlng, endLatlng)
                  .routeCalculationDescriptor(routeCalculationDescriptor)
                  .build();
          RouteCallback routeCallback = new RouteCallback() {
              @Override
              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;
                          }
                          densityList.add(density);
                          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){
                                  tripinfotext.setTextColor(Color.WHITE);
                              }else {
                                  tripinfotext.setTextColor(Color.BLACK);
                              }
                      }else {
                          tripinfotext.setTextColor(Colors[maxDensity]);
                      }
                  }
              }
    
              @Override
              public void onError(@NonNull RoutingException e) {
                  Log.e("routing ", "error: " + e.getLocalizedMessage());
              }
          };
          routingApi.planRoute(routeSpecification, routeCallback);
1 Like