libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/offroute/OffRouteDetector.java
private static boolean movingAwayFromManeuver(RouteProgress routeProgress,
                                                RingBuffer<Integer> distancesAwayFromManeuver,
                                                Point currentPoint) {

    if (routeProgress.currentLegProgress().upComingStep() == null) {
      return false;
    }

    double userDistanceToManeuver = TurfMeasurement.distance(
      routeProgress.currentLegProgress().upComingStep().maneuver().location(),
      currentPoint, TurfConstants.UNIT_METERS
    );

    boolean hasDistances = !distancesAwayFromManeuver.isEmpty();
    boolean validOffRouteDistanceTraveled = hasDistances && distancesAwayFromManeuver.peekLast()
      - distancesAwayFromManeuver.peekFirst() < MINIMUM_BACKUP_DISTANCE_FOR_OFF_ROUTE;
    boolean exceedsManeuverDistancesThreshold = validOffRouteDistanceTraveled
      && distancesAwayFromManeuver.size() >= 3;

    if (exceedsManeuverDistancesThreshold) {
      // User's moving away from maneuver position, thus offRoute.
      return true;
    }
    if (distancesAwayFromManeuver.isEmpty()) {
      distancesAwayFromManeuver.push((int) userDistanceToManeuver);
    } else if (userDistanceToManeuver > distancesAwayFromManeuver.peek()) {
      distancesAwayFromManeuver.push((int) userDistanceToManeuver);
    } else {
      // If we get a descending distance, reset the counter
      distancesAwayFromManeuver.clear();
    }
    return false;
  }
/**
   * Checks to see if the current point is moving away from the maneuver.
   * <p>
   * If the current point is farther away from the maneuver than the last point in the
   * stack, add it to the stack.
   * <p>
   * If the stack if >= 3 distances, return true to fire an off-route event as it
   * can be considered that the user is no longer going in the right direction.
   *
   * @param routeProgress             for the upcoming step maneuver
   * @param distancesAwayFromManeuver current stack of distances away
   * @param currentPoint              to determine if moving away or not
   * @return true if moving away from maneuver, false if not
   */
Checks to see if the current point is moving away from the maneuver.

If the current point is farther away from the maneuver than the last point in the
stack, add it to the stack.

If the stack if >= 3 distances, return true to fire an off-route event as it
can be considered that the user is no longer going in the right direction.

@param routeProgress             for the upcoming step maneuver
@param distancesAwayFromManeuver current stack of distances away
@param currentPoint              to determine if moving away or not
@return true if moving away from maneuver, false if not
Checks to see if the current point is moving away from the maneuver . If the current point is farther away from the maneuver than the last point in the stack add it to the stack . If the stack if > = 3 distances return true to fire an off - route event as it can be considered that the user is no longer going in the right direction . @param routeProgress for the upcoming step maneuver @param distancesAwayFromManeuver current stack of distances away @param currentPoint to determine if moving away or not @return true if moving away from maneuver false if not
private static boolean movingAwayFromManeuver(RouteProgress routeProgress,
                                                RingBuffer<Integer> distancesAwayFromManeuver,
                                                Point currentPoint) {
    if (routeProgress.currentLegProgress().upComingStep() == null) {
      return false;
    }
    double userDistanceToManeuver = TurfMeasurement.distance(
      routeProgress.currentLegProgress().upComingStep().maneuver().location(),
      currentPoint, TurfConstants.UNIT_METERS
    );
    boolean hasDistances = !distancesAwayFromManeuver.isEmpty();
    boolean validOffRouteDistanceTraveled = hasDistances && distancesAwayFromManeuver.peekLast()
      - distancesAwayFromManeuver.peekFirst() < MINIMUM_BACKUP_DISTANCE_FOR_OFF_ROUTE;
    boolean exceedsManeuverDistancesThreshold = validOffRouteDistanceTraveled
      && distancesAwayFromManeuver.size() >= 3;
    if (exceedsManeuverDistancesThreshold) {
      return true;
    }
    if (distancesAwayFromManeuver.isEmpty()) {
      distancesAwayFromManeuver.push((int) userDistanceToManeuver);
    } else if (userDistanceToManeuver > distancesAwayFromManeuver.peek()) {
      distancesAwayFromManeuver.push((int) userDistanceToManeuver);
    } else {
      distancesAwayFromManeuver.clear();
    }
    return false;
  }
private static boolean movingAwayFromManeuver ( RouteProgress routeProgress , RingBuffer < Integer > distancesAwayFromManeuver , Point currentPoint ) { if ( routeProgress . currentLegProgress ( ) . upComingStep ( ) == null ) { return false ; } double userDistanceToManeuver = TurfMeasurement . distance ( routeProgress . currentLegProgress ( ) . upComingStep ( ) . maneuver ( ) . location ( ) , currentPoint , TurfConstants . UNIT_METERS ) ; boolean hasDistances = ! distancesAwayFromManeuver . isEmpty ( ) ; boolean validOffRouteDistanceTraveled = hasDistances && distancesAwayFromManeuver . peekLast ( ) - distancesAwayFromManeuver . peekFirst ( ) < MINIMUM_BACKUP_DISTANCE_FOR_OFF_ROUTE ; boolean exceedsManeuverDistancesThreshold = validOffRouteDistanceTraveled && distancesAwayFromManeuver . size ( ) >= 3 ; if ( exceedsManeuverDistancesThreshold ) { return true ; } if ( distancesAwayFromManeuver . isEmpty ( ) ) { distancesAwayFromManeuver . push ( ( int ) userDistanceToManeuver ) ; } else if ( userDistanceToManeuver > distancesAwayFromManeuver . peek ( ) ) { distancesAwayFromManeuver . push ( ( int ) userDistanceToManeuver ) ; } else { distancesAwayFromManeuver . clear ( ) ; } return false ; }
Checks to see if the current point is moving away from the maneuver.
Checks to see if the current point is moving away from the maneuver .
// User's moving away from maneuver position, thus offRoute. // If we get a descending distance, reset the counter