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

The following examples show how to use com.mapbox.api.directions.v5.models.DirectionsRoute#distance() . 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
@Test
public void stepIntersections_includesAllStepIntersectionsAndNextManeuver() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  int stepIndex = 3;
  int legIndex = 0;
  double stepDistanceRemaining = 0;
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, legIndex);
  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

  int stepIntersections = route.legs().get(0).steps().get(3).intersections().size();
  int intersectionSize = stepIntersections + 1;

  assertEquals(intersectionSize, routeStepProgress.intersections().size());
}
 
Example 2
@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
@Test
public void distanceRemaining_equalsStepDistanceAtBeginning() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  LineString lineString = LineString.fromPolyline(firstLeg.steps().get(5).geometry(), Constants.PRECISION_6);
  double stepDistance = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS);

  double stepDistanceRemaining = firstLeg.steps().get(5).distance();
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  int stepIndex = 4;
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, 0);
  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

  assertEquals(stepDistance, routeStepProgress.distanceRemaining(), BaseTest.LARGE_DELTA);
}
 
Example 4
@Test
public void distanceTraveled_equalsStepDistanceAtEndOfStep() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  int stepIndex = 3;
  int legIndex = 0;
  double stepDistanceRemaining = 0;
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, legIndex);
  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

  assertEquals(firstLeg.steps().get(3).distance(),
    routeStepProgress.distanceTraveled(), BaseTest.DELTA);
}
 
Example 5
@Test
public void getDurationRemaining_equalsStepDurationAtBeginning() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  LegStep fourthStep = firstLeg.steps().get(5);
  double stepDuration = fourthStep.duration();
  int stepIndex = 5;
  int legIndex = 0;
  double stepDistanceRemaining = firstLeg.steps().get(stepIndex).distance();
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, legIndex);

  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
  double durationRemaining = routeStepProgress.durationRemaining();

  assertEquals(stepDuration, durationRemaining, BaseTest.DELTA);
}
 
Example 6
private void updateLastRerouteEvent(DirectionsRoute newDirectionsRoute) {
  if (!queuedRerouteEvents.isEmpty()) {
    RerouteEvent rerouteEvent = queuedRerouteEvents.get(queuedRerouteEvents.size() - 1);
    List<Point> geometryPositions = PolylineUtils.decode(newDirectionsRoute.geometry(), Constants.PRECISION_6);
    PolylineUtils.encode(geometryPositions, Constants.PRECISION_5);
    rerouteEvent.setNewRouteGeometry(PolylineUtils.encode(geometryPositions, Constants.PRECISION_5));
    int newDistanceRemaining = newDirectionsRoute.distance() == null ? 0 : newDirectionsRoute.distance().intValue();
    rerouteEvent.setNewDistanceRemaining(newDistanceRemaining);
    int newDurationRemaining = newDirectionsRoute.duration() == null ? 0 : newDirectionsRoute.duration().intValue();
    rerouteEvent.setNewDurationRemaining(newDurationRemaining);
  }
}
 
Example 7
private RouteProgress buildStepMilestoneRouteProgress() throws Exception {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(ROUTE_FIXTURE);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  DirectionsRoute route = response.routes().get(0);
  double distanceRemaining = route.distance();
  double legDistanceRemaining = route.legs().get(0).distance();
  double stepDistanceRemaining = route.legs().get(0).steps().get(0).distance();
  return buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, 1, 0);
}
 
Example 8
private RouteProgress buildTestRouteProgressForTrigger() throws Exception {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(ROUTE_FIXTURE);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);

  DirectionsRoute route = response.routes().get(0);
  double distanceRemaining = route.distance();
  double legDistanceRemaining = route.legs().get(0).distance();
  double stepDistanceRemaining = route.legs().get(0).steps().get(0).distance();
  return buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, 1, 0);
}
 
Example 9
@Test
public void dynamicRerouteDistanceTolerance_userCloseToIntersection() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteProgress routeProgress = buildDefaultTestRouteProgress();
  double distanceToIntersection = route.distance() - 39;
  LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  Point closePoint = TurfMeasurement.along(lineString, distanceToIntersection, TurfConstants.UNIT_METERS);

  double tolerance = ToleranceUtils.dynamicRerouteDistanceTolerance(closePoint, routeProgress);

  assertEquals(50.0, tolerance, DELTA);
}
 
Example 10
@Test
public void dynamicRerouteDistanceTolerance_userJustPastTheIntersection() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteProgress routeProgress = buildDefaultTestRouteProgress();
  double distanceToIntersection = route.distance();
  LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  Point closePoint = TurfMeasurement.along(lineString, distanceToIntersection, TurfConstants.UNIT_METERS);

  double tolerance = ToleranceUtils.dynamicRerouteDistanceTolerance(closePoint, routeProgress);

  assertEquals(50.0, tolerance, DELTA);
}
 
Example 11
private RouteProgress buildBeginningOfLegRouteProgress() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  double stepDistanceRemaining = route.legs().get(0).steps().get(0).distance();
  double legDistanceRemaining = route.legs().get(0).distance();
  double routeDistance = route.distance();
  return buildTestRouteProgress(route, stepDistanceRemaining, legDistanceRemaining,
    routeDistance, 0, 0);
}
 
Example 12
private RouteProgress buildBeginningOfLegRouteProgress(DirectionsRoute route) throws Exception {
  LegStep step = getFirstStep(route);
  double stepDistanceRemaining = step.distance();
  double legDistanceRemaining = route.legs().get(0).distance();
  double distanceRemaining = route.distance();
  return buildTestRouteProgress(route, stepDistanceRemaining, legDistanceRemaining,
    distanceRemaining, 0, 0);
}
 
Example 13
@Test
public void sanityTest() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  double stepDistanceRemaining = route.legs().get(0).steps().get(0).distance();
  double legDistanceRemaining = route.legs().get(0).distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining, legDistanceRemaining,
    distanceRemaining, 0, 0);

  assertNotNull(routeProgress.currentLegProgress().currentStepProgress());
}
 
Example 14
@Test
public void distanceRemaining_equalsCorrectValueAtIntervals() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  LegStep firstStep = route.legs().get(0).steps().get(0);
  LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
  double stepDistance = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS);
  double stepSegments = 5;

  for (double i = 0; i < stepDistance; i += stepSegments) {
    Point point = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS);

    if (point.equals(route.legs().get(0).steps().get(1).maneuver().location())) {
      return;
    }

    LineString slicedLine = TurfMisc.lineSlice(point,
      route.legs().get(0).steps().get(1).maneuver().location(), lineString);

    double stepDistanceRemaining = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
    double legDistanceRemaining = firstLeg.distance();
    double distanceRemaining = route.distance();
    RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
      legDistanceRemaining, distanceRemaining, 0, 0);
    RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

    assertEquals(stepDistanceRemaining, routeStepProgress.distanceRemaining(), BaseTest.DELTA);
  }
}
 
Example 15
@Test
public void distanceTraveled_equalsZeroAtBeginning() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  int stepIndex = 5;
  int legIndex = 0;
  double stepDistanceRemaining = firstLeg.steps().get(stepIndex).distance();
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, legIndex);
  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

  assertEquals(0, routeStepProgress.distanceTraveled(), BaseTest.DELTA);
}
 
Example 16
@Test
public void fractionTraveled_equalsZeroAtBeginning() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  int stepIndex = 5;
  int legIndex = 0;
  double stepDistanceRemaining = firstLeg.steps().get(stepIndex).distance();
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, legIndex);

  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
  assertEquals(0, routeStepProgress.fractionTraveled(), BaseTest.DELTA);
}
 
Example 17
@Test
public void fractionTraveled_equalsCorrectValueAtIntervals() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  LegStep firstStep = route.legs().get(0).steps().get(0);
  LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
  List<Float> fractionsRemaining = new ArrayList<>();
  List<Float> routeProgressFractionsTraveled = new ArrayList<>();
  double stepSegments = 5;

  for (double i = 0; i < firstStep.distance(); i += stepSegments) {
    Point point = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS);
    LineString slicedLine = TurfMisc.lineSlice(point,
      route.legs().get(0).steps().get(1).maneuver().location(), lineString);
    double stepDistanceRemaining = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
    int stepIndex = 0;
    int legIndex = 0;
    double legDistanceRemaining = firstLeg.distance();
    double distanceRemaining = route.distance();
    RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
      legDistanceRemaining, distanceRemaining, stepIndex, legIndex);
    RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
    float fractionRemaining = (float) ((firstStep.distance() - stepDistanceRemaining) / firstStep.distance());
    if (fractionRemaining < 0) {
      fractionRemaining = 0;
    }
    fractionsRemaining.add(fractionRemaining);
    routeProgressFractionsTraveled.add(routeStepProgress.fractionTraveled());
  }

  assertTrue(fractionsRemaining.equals(routeProgressFractionsTraveled));
}
 
Example 18
@Test
public void fractionTraveled_equalsOneAtEndOfStep() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  int stepIndex = 3;
  int legIndex = 0;
  double stepDistanceRemaining = 0;
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, legIndex);
  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

  assertEquals(1.0, routeStepProgress.fractionTraveled(), BaseTest.DELTA);
}
 
Example 19
@Test
public void durationRemaining_equalsZeroAtEndOfStep() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  int stepIndex = 3;
  int legIndex = 0;
  double stepDistanceRemaining = 0;
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, legIndex);
  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

  assertEquals(0, routeStepProgress.durationRemaining(), BaseTest.DELTA);
}
 
Example 20
private void obtainRouteData(DirectionsRoute route) {
  directionsRouteDistance = route.distance() != null ? route.distance().intValue() : 0;
  directionsRouteDuration = route.duration() != null ? route.duration().intValue() : 0;
  directionsRouteProfile = hasRouteProfile(route) ? route.routeOptions().profile() : "";
  directionsRouteDestination = retrieveRouteDestination(route);
}