Search in sources :

Example 16 with DirectionsResponse

use of com.mapbox.api.directions.v5.models.DirectionsResponse 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 17 with DirectionsResponse

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

the class RouteLegProgressTest method setup.

@Before
public void setup() throws IOException {
    Gson gson = new GsonBuilder().registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
    String body = loadJsonFixture(DIRECTIONS_PRECISION_6);
    DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
    route = response.routes().get(0);
    firstLeg = route.legs().get(0);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) DirectionsResponse(com.mapbox.api.directions.v5.models.DirectionsResponse) Before(org.junit.Before)

Example 18 with DirectionsResponse

use of com.mapbox.api.directions.v5.models.DirectionsResponse 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 19 with DirectionsResponse

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

the class NavigationMapRouteActivity method onMapReady.

@Override
public void onMapReady(MapboxMap mapboxMap) {
    this.mapboxMap = mapboxMap;
    navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap, "admin-3-4-boundaries-bg");
    Gson gson = new GsonBuilder().registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
    DirectionsResponse response = gson.fromJson(loadJsonFromAsset(DIRECTIONS_RESPONSE), DirectionsResponse.class);
    navigationMapRoute.addRoute(response.routes().get(0));
    mapboxMap.setOnMapLongClickListener(this);
    navigationMapRoute.setOnRouteSelectionChangeListener(this);
}
Also used : NavigationMapRoute(com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) DirectionsResponse(com.mapbox.api.directions.v5.models.DirectionsResponse)

Example 20 with DirectionsResponse

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

the class RouteViewModel method processRoute.

private void processRoute(@NonNull Response<DirectionsResponse> response) {
    if (isValidRoute(response)) {
        List<DirectionsRoute> routes = response.body().routes();
        DirectionsRoute bestRoute = routes.get(FIRST_ROUTE);
        DirectionsRoute chosenRoute = route.getValue();
        if (isNavigationRunning(chosenRoute)) {
            bestRoute = obtainMostSimilarRoute(routes, bestRoute, chosenRoute);
        }
        route.setValue(bestRoute);
    }
}
Also used : DirectionsRoute(com.mapbox.api.directions.v5.models.DirectionsRoute)

Aggregations

DirectionsResponse (com.mapbox.api.directions.v5.models.DirectionsResponse)17 Gson (com.google.gson.Gson)14 GsonBuilder (com.google.gson.GsonBuilder)14 DirectionsRoute (com.mapbox.api.directions.v5.models.DirectionsRoute)11 Before (org.junit.Before)9 Location (android.location.Location)3 Point (com.mapbox.geojson.Point)3 RouteLeg (com.mapbox.api.directions.v5.models.RouteLeg)2 BaseTest (com.mapbox.services.android.navigation.v5.BaseTest)2 FasterRoute (com.mapbox.services.android.navigation.v5.route.FasterRoute)2 RouteProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress)2 Test (org.junit.Test)2 LegStep (com.mapbox.api.directions.v5.models.LegStep)1 LineString (com.mapbox.geojson.LineString)1 NavigationMapRoute (com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute)1 NavigationRoute (com.mapbox.services.android.navigation.v5.navigation.NavigationRoute)1