Search in sources :

Example 6 with RouteStepProgress

use of com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress 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);
}
Also used : RouteStepProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress) LineString(com.mapbox.geojson.LineString) RouteLegProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress) Point(com.mapbox.geojson.Point)

Example 7 with RouteStepProgress

use of com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress in project mapbox-navigation-android by mapbox.

the class FasterRouteDetector method validStepDurationRemaining.

private boolean validStepDurationRemaining(RouteProgress routeProgress) {
    RouteStepProgress currentStepProgress = routeProgress.currentLegProgress().currentStepProgress();
    // Current step duration remaining in seconds
    int currentStepDurationRemaining = (int) currentStepProgress.durationRemaining();
    return currentStepDurationRemaining > NAVIGATION_MEDIUM_ALERT_DURATION;
}
Also used : RouteStepProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress)

Example 8 with RouteStepProgress

use of com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress in project mapbox-navigation-android by mapbox.

the class RouteStepProgressTest method distanceRemaining_equalsCorrectValueAtIntervals.

@Test
public void distanceRemaining_equalsCorrectValueAtIntervals() {
    LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
    double stepDistance = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS);
    // meters
    double stepSegments = 5;
    // Chop the line in small pieces
    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 distance = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
        RouteProgress routeProgress = RouteProgress.builder().stepDistanceRemaining(distance).legDistanceRemaining(firstLeg.distance()).distanceRemaining(route.distance()).directionsRoute(route).stepIndex(0).legIndex(0).build();
        RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
        assertEquals(distance, routeStepProgress.distanceRemaining(), BaseTest.DELTA);
    }
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Example 9 with RouteStepProgress

use of com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress in project mapbox-navigation-android by mapbox.

the class RouteStepProgressTest method stepIntersections_includesAllStepIntersectionsAndNextManeuver.

@Test
public void stepIntersections_includesAllStepIntersectionsAndNextManeuver() throws Exception {
    RouteProgress routeProgress = RouteProgress.builder().stepDistanceRemaining(0).legDistanceRemaining(firstLeg.distance()).distanceRemaining(route.distance()).directionsRoute(route).stepIndex(3).legIndex(0).build();
    RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
    int currentStepTotal = route.legs().get(0).steps().get(3).intersections().size();
    Point maneuverLocation = route.legs().get(0).steps().get(4).maneuver().location();
    assertEquals(currentStepTotal + 1, routeStepProgress.intersections().size());
    assertEquals(routeStepProgress.intersections().get(16).location().latitude(), maneuverLocation.latitude());
    assertEquals(routeStepProgress.intersections().get(16).location().longitude(), maneuverLocation.longitude());
}
Also used : Point(com.mapbox.geojson.Point) Point(com.mapbox.geojson.Point) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Example 10 with RouteStepProgress

use of com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress in project mapbox-navigation-android by mapbox.

the class RouteStepProgressTest method getDurationRemaining_equalsCorrectValueAtIntervals.

@Test
public void getDurationRemaining_equalsCorrectValueAtIntervals() {
    LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
    // meters
    double stepSegments = 5;
    // Chop the line in small pieces
    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 distance = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
        RouteProgress routeProgress = RouteProgress.builder().stepDistanceRemaining(distance).legDistanceRemaining(firstLeg.distance()).distanceRemaining(route.distance()).directionsRoute(route).stepIndex(0).legIndex(0).build();
        RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
        double fractionRemaining = (firstStep.distance() - distance) / firstStep.distance();
        assertEquals((1.0 - fractionRemaining) * firstStep.duration(), routeStepProgress.durationRemaining(), BaseTest.LARGE_DELTA);
    }
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Aggregations

BaseTest (com.mapbox.services.android.navigation.v5.BaseTest)8 Test (org.junit.Test)8 LineString (com.mapbox.geojson.LineString)7 Point (com.mapbox.geojson.Point)7 RouteStepProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 DirectionsResponse (com.mapbox.api.directions.v5.models.DirectionsResponse)1 DirectionsRoute (com.mapbox.api.directions.v5.models.DirectionsRoute)1 RouteLegProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress)1