Search in sources :

Example 31 with DirectionsRoute

use of com.mapbox.api.directions.v5.models.DirectionsRoute in project mapbox-navigation-android by mapbox.

the class FasterRouteDetector method isFasterRoute.

@Override
public boolean isFasterRoute(DirectionsResponse response, RouteProgress routeProgress) {
    if (validRouteResponse(response)) {
        double currentDurationRemaining = routeProgress.durationRemaining();
        DirectionsRoute newRoute = response.routes().get(0);
        if (hasLegs(newRoute)) {
            // Extract the first leg
            RouteLeg routeLeg = newRoute.legs().get(0);
            if (hasAtLeastTwoSteps(routeLeg)) {
                // Extract the first two steps
                LegStep firstStep = routeLeg.steps().get(0);
                LegStep secondStep = routeLeg.steps().get(1);
                // Check for valid first and second steps of the new route
                if (!validFirstStep(firstStep) || !validSecondStep(secondStep, routeProgress)) {
                    return false;
                }
            }
        }
        // New route must be at least 10% faster
        if (newRoute.duration() <= (0.9 * currentDurationRemaining)) {
            return true;
        }
    }
    return false;
}
Also used : DirectionsRoute(com.mapbox.api.directions.v5.models.DirectionsRoute) RouteLeg(com.mapbox.api.directions.v5.models.RouteLeg) LegStep(com.mapbox.api.directions.v5.models.LegStep)

Example 32 with DirectionsRoute

use of com.mapbox.api.directions.v5.models.DirectionsRoute in project mapbox-navigation-android by mapbox.

the class MetricsRouteProgress method retrieveRouteDestination.

private Point retrieveRouteDestination(DirectionsRoute route) {
    RouteLeg lastLeg = route.legs().get(route.legs().size() - 1);
    LegStep lastStep = lastLeg.steps().get(lastLeg.steps().size() - 1);
    StepManeuver finalManuever = lastStep.maneuver();
    if (finalManuever.location() != null) {
        return finalManuever.location();
    }
    return Point.fromLngLat(0d, 0d);
}
Also used : StepManeuver(com.mapbox.api.directions.v5.models.StepManeuver) RouteLeg(com.mapbox.api.directions.v5.models.RouteLeg) LegStep(com.mapbox.api.directions.v5.models.LegStep)

Example 33 with DirectionsRoute

use of com.mapbox.api.directions.v5.models.DirectionsRoute in project mapbox-navigation-android by mapbox.

the class RouteProgressTest method multiLeg_getFractionTraveled_equalsCorrectValueAtIntervals.

// TODO check fut
@Test
public void multiLeg_getFractionTraveled_equalsCorrectValueAtIntervals() {
    // Chop the line in small pieces
    for (RouteLeg leg : multiLegRoute.legs()) {
        for (int step = 0; step < leg.steps().size(); step++) {
            RouteProgress routeProgress = RouteProgress.builder().stepDistanceRemaining(multiLegRoute.legs().get(0).steps().get(0).distance()).legDistanceRemaining(multiLegRoute.legs().get(0).distance()).distanceRemaining(multiLegRoute.distance()).directionsRoute(multiLegRoute).stepIndex(step).legIndex(0).build();
            float fractionRemaining = (float) (routeProgress.distanceTraveled() / multiLegRoute.distance());
            assertEquals(fractionRemaining, routeProgress.fractionTraveled(), BaseTest.LARGE_DELTA);
        }
    }
}
Also used : RouteLeg(com.mapbox.api.directions.v5.models.RouteLeg) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Example 34 with DirectionsRoute

use of com.mapbox.api.directions.v5.models.DirectionsRoute in project mapbox-navigation-android by mapbox.

the class MockNavigationActivity method calculateRoute.

private void calculateRoute() {
    Location userLocation = mapboxMap.getMyLocation();
    if (userLocation == null) {
        Timber.d("calculateRoute: User location is null, therefore, origin can't be set.");
        return;
    }
    Point origin = Point.fromLngLat(userLocation.getLongitude(), userLocation.getLatitude());
    if (TurfMeasurement.distance(origin, destination, TurfConstants.UNIT_METERS) < 50) {
        startRouteButton.setVisibility(View.GONE);
        return;
    }
    final NavigationRoute.Builder navigationRouteBuilder = NavigationRoute.builder().accessToken(Mapbox.getAccessToken());
    navigationRouteBuilder.origin(origin);
    navigationRouteBuilder.destination(destination);
    if (waypoint != null) {
        navigationRouteBuilder.addWaypoint(waypoint);
    }
    navigationRouteBuilder.build().getRoute(new Callback<DirectionsResponse>() {

        @Override
        public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
            Timber.d("Url: %s", call.request().url().toString());
            if (response.body() != null) {
                if (!response.body().routes().isEmpty()) {
                    DirectionsRoute directionsRoute = response.body().routes().get(0);
                    MockNavigationActivity.this.route = directionsRoute;
                    navigationMapRoute.addRoutes(response.body().routes());
                }
            }
        }

        @Override
        public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
            Timber.e("onFailure: navigation.getRoute()", throwable);
        }
    });
}
Also used : DirectionsRoute(com.mapbox.api.directions.v5.models.DirectionsRoute) Point(com.mapbox.geojson.Point) NavigationRoute(com.mapbox.services.android.navigation.v5.navigation.NavigationRoute) Location(android.location.Location) DirectionsResponse(com.mapbox.api.directions.v5.models.DirectionsResponse)

Example 35 with DirectionsRoute

use of com.mapbox.api.directions.v5.models.DirectionsRoute in project mapbox-navigation-android by mapbox.

the class NavigationMapRoute method waypointFeatureCollection.

/**
 * The routes also display an icon for each waypoint in the route, we use symbol layers for this.
 */
private static 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);
}
Also used : ArrayList(java.util.ArrayList) RouteLeg(com.mapbox.api.directions.v5.models.RouteLeg) Feature(com.mapbox.services.commons.geojson.Feature)

Aggregations

DirectionsRoute (com.mapbox.api.directions.v5.models.DirectionsRoute)25 DirectionsResponse (com.mapbox.api.directions.v5.models.DirectionsResponse)14 Gson (com.google.gson.Gson)11 GsonBuilder (com.google.gson.GsonBuilder)11 BaseTest (com.mapbox.services.android.navigation.v5.BaseTest)10 RouteProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress)10 Test (org.junit.Test)10 RouteLeg (com.mapbox.api.directions.v5.models.RouteLeg)9 Before (org.junit.Before)7 Point (com.mapbox.geojson.Point)6 Location (android.location.Location)3 LegStep (com.mapbox.api.directions.v5.models.LegStep)3 LineString (com.mapbox.geojson.LineString)3 FasterRoute (com.mapbox.services.android.navigation.v5.route.FasterRoute)2 Feature (com.mapbox.services.commons.geojson.Feature)2 ArrayList (java.util.ArrayList)2 StepManeuver (com.mapbox.api.directions.v5.models.StepManeuver)1 NavigationRoute (com.mapbox.services.android.navigation.v5.navigation.NavigationRoute)1 LineString (com.mapbox.services.commons.geojson.LineString)1 Point (com.mapbox.services.commons.geojson.Point)1