use of com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress in project mapbox-navigation-android by mapbox.
the class SnapToRoute method snapLocationBearing.
/**
* Creates a snapped bearing for the snapped {@link Location}.
* <p>
* This is done by measuring 1 meter ahead of the current step distance traveled and
* creating a {@link Point} with this distance using {@link TurfMeasurement#along(LineString, double, String)}.
* <p>
* If the step distance remaining is zero, the distance ahead is 1 meter into the upcoming step.
* This way, an accurate bearing is upheld transitioning between steps.
*
* @param routeProgress for all current progress values
* @return float bearing snapped to route
*/
private static float snapLocationBearing(RouteProgress routeProgress) {
RouteLegProgress legProgress = routeProgress.currentLegProgress();
RouteStepProgress stepProgress = legProgress.currentStepProgress();
double distanceTraveled = stepProgress.distanceTraveled();
double distanceRemaining = stepProgress.distanceRemaining();
boolean distanceRemainingZero = distanceRemaining == 0;
// Either want to measure our current step distance traveled + 1 or 1 meter into the upcoming step
double distanceAhead = distanceRemainingZero ? 1 : distanceTraveled + 1;
// Create the step linestring from the geometry
LineString upcomingLineString = createUpcomingLineString(legProgress, distanceRemainingZero);
LineString currentLineString = createCurrentLineString(legProgress);
// Measure 1 meter ahead of the users current location, only if the distance remaining isn't zero
Point futurePoint = createFuturePoint(distanceAhead, upcomingLineString, currentLineString);
Point currentPoint = TurfMeasurement.along(currentLineString, distanceTraveled, TurfConstants.UNIT_METERS);
// Get bearing and convert azimuth to degrees
double azimuth = TurfMeasurement.bearing(currentPoint, futurePoint);
return (float) MathUtils.wrap(azimuth, 0, 360);
}
Aggregations