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;
}
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);
}
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);
}
});
}
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);
}
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);
}
}
Aggregations