Java Code Examples for com.mapbox.api.directions.v5.models.DirectionsRoute#legs()

The following examples show how to use com.mapbox.api.directions.v5.models.DirectionsRoute#legs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NavigationHelper.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
/**
 * Given the current {@link DirectionsRoute} and leg / step index,
 * return a list of {@link Point} representing the current step.
 * <p>
 * This method is only used on a per-step basis as {@link PolylineUtils#decode(String, int)}
 * can be a heavy operation based on the length of the step.
 * <p>
 * Returns null if index is invalid.
 *
 * @param directionsRoute for list of steps
 * @param legIndex        to get current step list
 * @param stepIndex       to get current step
 * @return list of {@link Point} representing the current step
 */
static List<Point> decodeStepPoints(DirectionsRoute directionsRoute, List<Point> currentPoints,
                                    int legIndex, int stepIndex) {
  List<RouteLeg> legs = directionsRoute.legs();
  if (hasInvalidLegs(legs)) {
    return currentPoints;
  }
  List<LegStep> steps = legs.get(legIndex).steps();
  if (hasInvalidSteps(steps)) {
    return currentPoints;
  }
  boolean invalidStepIndex = stepIndex < 0 || stepIndex > steps.size() - 1;
  if (invalidStepIndex) {
    return currentPoints;
  }
  LegStep step = steps.get(stepIndex);
  if (step == null) {
    return currentPoints;
  }
  String stepGeometry = step.geometry();
  if (stepGeometry != null) {
    return PolylineUtils.decode(stepGeometry, PRECISION_6);
  }
  return currentPoints;
}
 
Example 2
Source File: RouteProgressTest.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
@Test
public void multiLeg_getFractionTraveled_equalsCorrectValueAtIntervals() throws Exception {
  DirectionsRoute multiLegRoute = buildMultipleLegRoute();
  List<Float> fractionsRemaining = new ArrayList<>();
  List<Float> routeProgressFractionsTraveled = new ArrayList<>();

  for (RouteLeg leg : multiLegRoute.legs()) {
    for (int stepIndex = 0; stepIndex < leg.steps().size(); stepIndex++) {
      double stepDistanceRemaining = getFirstStep(multiLegRoute).distance();
      double legDistanceRemaining = multiLegRoute.legs().get(0).distance();
      double distanceRemaining = multiLegRoute.distance();
      RouteProgress routeProgress = buildTestRouteProgress(multiLegRoute, stepDistanceRemaining,
        legDistanceRemaining, distanceRemaining, stepIndex, 0);
      float fractionRemaining = (float) (routeProgress.distanceTraveled() / multiLegRoute.distance());

      fractionsRemaining.add(fractionRemaining);
      routeProgressFractionsTraveled.add(routeProgress.fractionTraveled());
    }
  }

  assertTrue(fractionsRemaining.equals(routeProgressFractionsTraveled));
}
 
Example 3
Source File: ViewRouteFetcher.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private String obtainRouteLegDescriptionFrom(DirectionsRoute route) {
  List<RouteLeg> routeLegs = route.legs();
  StringBuilder routeLegDescription = new StringBuilder();
  for (RouteLeg leg : routeLegs) {
    routeLegDescription.append(leg.summary());
  }
  return routeLegDescription.toString();
}
 
Example 4
Source File: NavigationMapRoute.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * The routes also display an icon for each waypoint in the route, we use symbol layers for this.
 */
private FeatureCollection waypointFeatureCollection(DirectionsRoute route) {
  final List<Feature> waypointFeatures = new ArrayList<>();
  for (RouteLeg leg : route.legs()) {
    waypointFeatures.add(getPointFromLineString(leg, 0));
    waypointFeatures.add(getPointFromLineString(leg, leg.steps().size() - 1));
  }
  return FeatureCollection.fromFeatures(waypointFeatures);
}
 
Example 5
Source File: FasterRouteDetector.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private boolean hasLegs(DirectionsRoute newRoute) {
  return newRoute.legs() != null && !newRoute.legs().isEmpty();
}