com.mapbox.api.directions.v5.models.DirectionsResponse Java Examples

The following examples show how to use com.mapbox.api.directions.v5.models.DirectionsResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void postIsUsed() {
  MapboxDirections.Builder builder = MapboxDirections.builder()
    .profile(PROFILE_CYCLING)
    .steps(true)
    .origin(Point.fromLngLat(-122.42,37.78))
    .destination(Point.fromLngLat(-77.03,38.91))
    .voiceInstructions(true)
    .voiceUnits(DirectionsCriteria.IMPERIAL)
    .accessToken(ACCESS_TOKEN)
    .baseUrl(mockUrl.toString())
    .post();

  retrofit2.Call<DirectionsResponse> call = builder.build().initializeCall();

  assertEquals("POST", call.request().method());
}
 
Example #2
Source Project: graphhopper-navigation-android   Author: graphhopper   File: RerouteActivity.java    License: MIT License 6 votes vote down vote up
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
  Timber.d(call.request().url().toString());
  if (response.body() != null) {
    if (!response.body().routes().isEmpty()) {
      // Extract the route
      DirectionsRoute route = response.body().routes().get(0);
      // Draw it on the map
      drawRoute(route);
      // Start mocking the new route
      resetLocationEngine(route);
      navigation.startNavigation(route);
      mapboxMap.setOnMapClickListener(this);
      tracking = true;
    }
  }
}
 
Example #3
Source Project: mapbox-java   Author: mapbox   File: BasicDirectionsRefresh.java    License: MIT License 6 votes vote down vote up
private static String simpleMapboxDirectionsRequest() throws IOException {
  MapboxDirections directions = MapboxDirections.builder()
    .accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
    .enableRefresh(true)
    .origin(Point.fromLngLat(-95.6332, 29.7890))
    .destination(Point.fromLngLat(-95.3591, 29.7576))
    .overview(OVERVIEW_FULL)
    .profile(PROFILE_DRIVING_TRAFFIC)
    .addAnnotation(ANNOTATION_CONGESTION).build();

  Response<DirectionsResponse> response = directions.executeCall();
  System.out.println("Directions response: " + response);
  String requestId = response.body().routes().get(0).routeOptions().requestUuid();

  return requestId;
}
 
Example #4
Source Project: graphhopper-navigation-android   Author: graphhopper   File: NavigationLauncherActivity.java    License: MIT License 6 votes vote down vote up
private void fetchRoute() {
  NavigationRoute.Builder builder = NavigationRoute.builder(this)
    .accessToken(Mapbox.getAccessToken())
    .origin(currentLocation)
    .destination(destination)
    .alternatives(true);
  setFieldsFromSharedPreferences(builder);
  builder.build()
    .getRoute(new SimplifiedCallback() {
      @Override
      public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
        if (validRouteResponse(response)) {
          hideLoading();
          route = response.body().routes().get(0);
          if (route.distance() > 25d) {
            launchRouteBtn.setEnabled(true);
            mapRoute.addRoutes(response.body().routes());
            boundCameraToRoute();
          } else {
            Snackbar.make(mapView, R.string.error_select_longer_route, Snackbar.LENGTH_SHORT).show();
          }
        }
      }
    });
  loading.setVisibility(View.VISIBLE);
}
 
Example #5
Source Project: mapbox-java   Author: mapbox   File: BasicDirections.java    License: MIT License 6 votes vote down vote up
/**
 * Demonstrates how to make the most basic POST directions request.
 *
 * @throws IOException signals that an I/O exception of some sort has occurred
 */
private static void simpleMapboxDirectionsPostRequest() throws IOException {

  MapboxDirections.Builder builder = MapboxDirections.builder();

  builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
  builder.origin(Point.fromLngLat(-95.6332, 29.7890));
  builder.destination(Point.fromLngLat(-95.3591, 29.7576));
  builder.post();

  Response<DirectionsResponse> response = builder.build().executeCall();

  System.out.printf("%nCheck that the POST response is successful %b", response.isSuccessful());
  System.out.printf("%nGet the first routes distance from origin to destination: %f",
    response.body().routes().get(0).distance());
}
 
Example #6
Source Project: mapbox-java   Author: mapbox   File: BasicDirections.java    License: MIT License 6 votes vote down vote up
/**
 * Demonstrates how to make the most basic GET directions request.
 *
 * @throws IOException signals that an I/O exception of some sort has occurred
 */
private static void simpleMapboxDirectionsRequest() throws IOException {

  MapboxDirections.Builder builder = MapboxDirections.builder();

  // 1. Pass in all the required information to get a simple directions route.
  builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
  builder.origin(Point.fromLngLat(-95.6332, 29.7890));
  builder.destination(Point.fromLngLat(-95.3591, 29.7576));

  // 2. That's it! Now execute the command and get the response.
  Response<DirectionsResponse> response = builder.build().executeCall();

  // 3. Log information from the response
  System.out.printf("Check that the GET response is successful %b", response.isSuccessful());
  System.out.printf("%nGet the first routes distance from origin to destination: %f",
    response.body().routes().get(0).distance());
}
 
Example #7
Source Project: graphhopper-navigation-android   Author: graphhopper   File: NavigationEventDispatcherTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
  MockitoAnnotations.initMocks(this);
  Context context = mock(Context.class);
  when(context.getApplicationContext()).thenReturn(mock(Context.class));
  navigation = new MapboxNavigation(context, ACCESS_TOKEN, mock(NavigationTelemetry.class),
    mock(LocationEngine.class));
  navigationEventDispatcher = navigation.getEventDispatcher();

  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(PRECISION_6);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  route = response.routes().get(0);

  routeProgress = buildTestRouteProgress(route, 100, 100, 100, 0, 0);
}
 
Example #8
Source Project: graphhopper-navigation-android   Author: graphhopper   File: FasterRouteDetectorTest.java    License: MIT License 6 votes vote down vote up
@Test
public void onFasterRouteResponse_isFasterRouteIsTrue() throws Exception {
  MapboxNavigation navigation = buildNavigationWithFasterRouteEnabled();
  FasterRoute fasterRouteEngine = navigation.getFasterRouteEngine();
  RouteProgress currentProgress = obtainDefaultRouteProgress();
  DirectionsRoute longerRoute = currentProgress.directionsRoute().toBuilder()
    .duration(10000000d)
    .build();
  currentProgress = currentProgress.toBuilder()
    .directionsRoute(longerRoute)
    .build();
  DirectionsResponse response = obtainADirectionsResponse();

  boolean isFasterRoute = fasterRouteEngine.isFasterRoute(response, currentProgress);

  assertTrue(isFasterRoute);
}
 
Example #9
Source Project: graphhopper-navigation-android   Author: graphhopper   File: FasterRouteDetectorTest.java    License: MIT License 6 votes vote down vote up
@Test
public void onSlowerRouteResponse_isFasterRouteIsFalse() throws Exception {
  MapboxNavigation navigation = buildNavigationWithFasterRouteEnabled();
  FasterRoute fasterRouteEngine = navigation.getFasterRouteEngine();
  RouteProgress currentProgress = obtainDefaultRouteProgress();
  DirectionsRoute longerRoute = currentProgress.directionsRoute().toBuilder()
    .duration(1000d)
    .build();
  currentProgress = currentProgress.toBuilder()
    .directionsRoute(longerRoute)
    .build();
  DirectionsResponse response = obtainADirectionsResponse();

  boolean isFasterRoute = fasterRouteEngine.isFasterRoute(response, currentProgress);

  assertFalse(isFasterRoute);
}
 
Example #10
Source Project: mapbox-java   Author: mapbox   File: DirectionsResponseFactory.java    License: MIT License 6 votes vote down vote up
Response<DirectionsResponse> generate(Response<DirectionsResponse> response) {
  if (isNotSuccessful(response)) {
    return response;
  } else {
    return Response.success(
      response
        .body()
        .toBuilder()
        .routes(generateRouteOptions(response))
        .build(),
      new okhttp3.Response.Builder()
        .code(200)
        .message("OK")
        .protocol(response.raw().protocol())
        .headers(response.headers())
        .request(response.raw().request())
        .build());
  }
}
 
Example #11
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void callFactoryNonNull() throws IOException {
  MapboxDirections client = MapboxDirections.builder()
    .accessToken(ACCESS_TOKEN)
    .origin(Point.fromLngLat(1.0, 2.0))
    .destination(Point.fromLngLat(5.0, 6.0))
    .baseUrl(mockUrl.toString())
    .build();

  // Setting a null call factory doesn't make the request fail
  // (the default OkHttp client is used)
  client.setCallFactory(null);
  Response<DirectionsResponse> response = client.executeCall();
  assertEquals(200, response.code());
  assertEquals("Ok", response.body().code());
}
 
Example #12
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void subBannerInstructions() throws Exception {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(DIRECTIONS_V5_BANNER_INSTRUCTIONS);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);

  BannerInstructions bannerInstructions =
    response.routes().get(0).legs().get(0).steps().get(0).bannerInstructions().get(1);

  assertNotNull(bannerInstructions.sub());
  assertNotNull(bannerInstructions.sub().components());

  BannerComponents component = bannerInstructions.sub().components().get(1);
  assertNotNull(component.active());
  assertNotNull(component.directions());
  assertEquals(2, component.directions().size());
}
 
Example #13
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void approachesList() throws Exception {
  List<String> approaches = new ArrayList<>();
  approaches.add(APPROACH_UNRESTRICTED);
  approaches.add(APPROACH_CURB);

  MapboxDirections mapboxDirections = MapboxDirections.builder()
          .profile(PROFILE_DRIVING)
          .origin(Point.fromLngLat(13.4301,52.5109))
          .destination(Point.fromLngLat(13.432508,52.501725))
          .approaches(approaches)
          .accessToken(ACCESS_TOKEN)
          .baseUrl(mockUrl.toString())
          .build();

  mapboxDirections.setCallFactory(null);
  Response<DirectionsResponse> response = mapboxDirections.executeCall();
  assertEquals(200, response.code());
  assertEquals("Ok", response.body().code());
}
 
Example #14
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void routeOptionsApproachesList() throws Exception {
  List<String> approaches = new ArrayList<>();
  approaches.add(APPROACH_UNRESTRICTED);
  approaches.add(APPROACH_CURB);

  MapboxDirections mapboxDirections = MapboxDirections.builder()
          .profile(PROFILE_DRIVING)
          .origin(Point.fromLngLat(13.4301,52.5109))
          .destination(Point.fromLngLat(13.432508,52.501725))
          .approaches(approaches)
          .accessToken(ACCESS_TOKEN)
          .baseUrl(mockUrl.toString())
          .build();

  mapboxDirections.setCallFactory(null);
  Response<DirectionsResponse> response = mapboxDirections.executeCall();
  RouteOptions routeOptions = response.body().routes().get(0).routeOptions();


  List<String> result = routeOptions.approachesList();
  assertEquals("unrestricted", result.get(0));
  assertEquals("curb", result.get(1));
}
 
Example #15
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void withWaypointNames() throws Exception {
  List<String> names = new ArrayList<>();
  names.add("Home");
  names.add("Work");

  MapboxDirections mapboxDirections = MapboxDirections.builder()
    .profile(PROFILE_CYCLING)
    .origin(Point.fromLngLat(-122.42,37.78))
    .destination(Point.fromLngLat(-77.03,38.91))
    .steps(true)
    .voiceInstructions(true)
    .voiceUnits(DirectionsCriteria.IMPERIAL)
    .waypointNames(names)
    .accessToken(ACCESS_TOKEN)
    .baseUrl(mockUrl.toString())
    .build();

  mapboxDirections.setCallFactory(null);
  Response<DirectionsResponse> response = mapboxDirections.executeCall();
  assertEquals(200, response.code());
  assertEquals("Ok", response.body().code());
}
 
Example #16
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void getIsUsed() {
  MapboxDirections.Builder builder = MapboxDirections.builder()
    .profile(PROFILE_CYCLING)
    .steps(true)
    .origin(Point.fromLngLat(-122.42,37.78))
    .destination(Point.fromLngLat(-77.03,38.91))
    .voiceInstructions(true)
    .voiceUnits(DirectionsCriteria.IMPERIAL)
    .accessToken(ACCESS_TOKEN)
    .baseUrl(mockUrl.toString())
    .get();

  retrofit2.Call<DirectionsResponse> call = builder.build().initializeCall();

  assertEquals("GET", call.request().method());
}
 
Example #17
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void withWaypointTargets() throws Exception {
  List<Point> targets = new ArrayList<>();
  targets.add(null);
  targets.add( Point.fromLngLat(-6.799936294555664,61.99987216574813));

  MapboxDirections mapboxDirections = MapboxDirections.builder()
          .profile(PROFILE_DRIVING_TRAFFIC)
          .origin(Point.fromLngLat(-6.80904429026134,62.00015328799685))
          .destination(Point.fromLngLat(-6.800065040588378,62.00012400993553))
          .steps(true)
          .waypointTargets(targets)
          .accessToken(ACCESS_TOKEN)
          .baseUrl(mockUrl.toString())
          .build();

  assertNotNull(mapboxDirections);
  assertNotNull(mapboxDirections.cloneCall().request().url().queryParameter("waypoint_targets"));

  Response<DirectionsResponse> response = mapboxDirections.executeCall();
  assertEquals(200, response.code());
  assertEquals("Ok", response.body().code());
  assertEquals("left",
          response.body().routes().get(0).legs().get(0).steps().get(0).maneuver().modifier());

}
 
Example #18
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void post() throws IOException {
  List<String> names = new ArrayList<>();
  names.add("Home");
  names.add("Work");

  MapboxDirections mapboxDirections = MapboxDirections.builder()
    .profile(PROFILE_CYCLING)
    .origin(Point.fromLngLat(-122.42,37.78))
    .destination(Point.fromLngLat(-77.03,38.91))
    .steps(true)
    .voiceInstructions(true)
    .voiceUnits(DirectionsCriteria.IMPERIAL)
    .waypointNames(names)
    .accessToken(ACCESS_TOKEN)
    .baseUrl(mockUrl.toString())
    .post()
    .build();

  Response<DirectionsResponse> response = mapboxDirections.executeCall();
  assertEquals(200, response.code());
  assertEquals("Ok", response.body().code());

  assertNotNull(response.body().routes());
  assertEquals(1, response.body().routes().size());
}
 
Example #19
Source Project: mapbox-java   Author: mapbox   File: BasicDirections.java    License: MIT License 6 votes vote down vote up
/**
 * Demonstrates how to make the most basic GET directions request using the walking profile.
 *
 * @throws IOException signals that an I/O exception of some sort has occurred
 */
private static void simpleMapboxDirectionsWalkingRequest() throws IOException {

  MapboxDirections.Builder builder = MapboxDirections.builder();

  builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
  builder.origin(Point.fromLngLat(-95.6332, 29.7890));
  builder.destination(Point.fromLngLat(-95.3591, 29.7576));
  builder.profile("walking");
  builder.walkingOptions(
    WalkingOptions.builder()
      .walkingSpeed(1.0)
      .walkwayBias(0.6)
      .alleyBias(0.7)
      .build());

  // 2. That's it! Now execute the command and get the response.
  Response<DirectionsResponse> response = builder.build().executeCall();

  // 3. Log information from the response
  System.out.printf("%nCheck that the GET response is successful %b", response.isSuccessful());
  System.out.printf("%nGet the first routes distance from origin to destination: %f",
    response.body().routes().get(0).distance());
}
 
Example #20
Source Project: mapbox-java   Author: mapbox   File: MapboxDirectionsTest.java    License: MIT License 6 votes vote down vote up
@Test
public void callForUrlLength_shortUrl() {
  MapboxDirections.Builder builder = MapboxDirections.builder()
    .profile(PROFILE_CYCLING)
    .steps(true)
    .origin(Point.fromLngLat(-122.42,37.78))
    .destination(Point.fromLngLat(-77.03,38.91))
    .voiceInstructions(true)
    .voiceUnits(DirectionsCriteria.IMPERIAL)
    .accessToken(ACCESS_TOKEN)
    .baseUrl(mockUrl.toString());
  addWaypoints(builder, 10);

  retrofit2.Call<DirectionsResponse> call = builder.build().initializeCall();

  assertEquals("GET", call.request().method());
}
 
Example #21
Source Project: graphhopper-navigation-android   Author: graphhopper   File: ViewRouteFetcher.java    License: MIT License 5 votes vote down vote up
private void processRoute(@NonNull DirectionsResponse response) {
  if (isValidRoute(response)) {
    List<DirectionsRoute> routes = response.routes();
    DirectionsRoute bestRoute = routes.get(FIRST_ROUTE);
    DirectionsRoute chosenRoute = currentRoute;
    if (isNavigationRunning(chosenRoute)) {
      bestRoute = obtainMostSimilarRoute(routes, bestRoute, chosenRoute);
    }
    updateCurrentRoute(bestRoute);
  }
}
 
Example #22
Source Project: graphhopper-navigation-android   Author: graphhopper   File: DynamicCameraTest.java    License: MIT License 5 votes vote down vote up
private DirectionsRoute buildDirectionsRoute() throws IOException {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(DIRECTIONS_PRECISION_6);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  return response.routes().get(0);
}
 
Example #23
Source Project: graphhopper-navigation-android   Author: graphhopper   File: TestRouteBuilder.java    License: MIT License 5 votes vote down vote up
DirectionsRoute buildTestDirectionsRoute(@Nullable String fixtureName) throws IOException {
  fixtureName = checkNullFixtureName(fixtureName);
  Gson gson = new GsonBuilder().registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(fixtureName);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  DirectionsRoute route = response.routes().get(0);
  return buildRouteWithOptions(route);
}
 
Example #24
Source Project: graphhopper-navigation-android   Author: graphhopper   File: ViewRouteFetcherTest.java    License: MIT License 5 votes vote down vote up
@Test
public void onRouteResponseReceived_routeUpdateCallbackIsCalled() throws Exception {
  ViewRouteListener routeEngineListener = mock(ViewRouteListener.class);
  ViewRouteFetcher routeEngine = buildRouteEngine(routeEngineListener);
  DirectionsResponse response = buildDirectionsResponse();
  DirectionsRoute route = response.routes().get(0);
  RouteProgress routeProgress = mock(RouteProgress.class);

  routeEngine.onResponseReceived(response, routeProgress);

  verify(routeEngineListener).onRouteUpdate(route);
}
 
Example #25
Source Project: graphhopper-navigation-android   Author: graphhopper   File: ViewRouteFetcherTest.java    License: MIT License 5 votes vote down vote up
private RouteOptions buildRouteOptionsWithCoordinates(DirectionsResponse response) {
  List<Point> coordinates = new ArrayList<>();
  for (DirectionsWaypoint waypoint : response.waypoints()) {
    coordinates.add(waypoint.location());
  }
  return RouteOptions.builder()
    .baseUrl(Constants.BASE_API_URL)
    .user("user")
    .profile("profile")
    .accessToken(ACCESS_TOKEN)
    .requestUuid("uuid")
    .geometries("mocked_geometries")
    .coordinates(coordinates).build();
}
 
Example #26
Source Project: graphhopper-navigation-android   Author: graphhopper   File: NavigationFragment.java    License: MIT License 5 votes vote down vote up
private void fetchRoute(Point origin, Point destination) {
  NavigationRoute.builder(getContext())
    .accessToken(Mapbox.getAccessToken())
    .origin(origin)
    .destination(destination)
    .build()
    .getRoute(new SimplifiedCallback() {
      @Override
      public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
        directionsRoute = response.body().routes().get(0);
        startNavigation();
      }
    });
}
 
Example #27
Source Project: graphhopper-navigation-android   Author: graphhopper   File: DualNavigationMapActivity.java    License: MIT License 5 votes vote down vote up
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
  if (validRouteResponse(response)) {
    updateLoadingTo(false);
    launchNavigationFab.show();
    route = response.body().routes().get(0);
    mapRoute.addRoutes(response.body().routes());
    if (isNavigationRunning) {
      launchNavigation();
    }
  }
}
 
Example #28
Source Project: graphhopper-navigation-android   Author: graphhopper   File: WaypointNavigationActivity.java    License: MIT License 5 votes vote down vote up
private void fetchRoute(Point origin, Point destination) {
  NavigationRoute.builder(this)
    .accessToken(Mapbox.getAccessToken())
    .origin(origin)
    .destination(destination)
    .alternatives(true)
    .build()
    .getRoute(new SimplifiedCallback() {
      @Override
      public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
        startNavigation(response.body().routes().get(0));
      }
    });
}
 
Example #29
Source Project: graphhopper-navigation-android   Author: graphhopper   File: EndNavigationActivity.java    License: MIT License 5 votes vote down vote up
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
  if (validRouteResponse(response)) {
    updateLoadingTo(false);
    message.setText("Launch Navigation");
    launchNavigationFab.setVisibility(View.VISIBLE);
    launchNavigationFab.show();
    route = response.body().routes().get(0);
    if (isNavigationRunning) {
      launchNavigation();
    }
  }
}
 
Example #30
Source Project: graphhopper-navigation-android   Author: graphhopper   File: ComponentNavigationActivity.java    License: MIT License 5 votes vote down vote up
private void handleRoute(Response<DirectionsResponse> response, boolean isOffRoute) {
  List<DirectionsRoute> routes = response.body().routes();
  if (!routes.isEmpty()) {
    route = routes.get(FIRST);
    navigationMap.drawRoute(route);
    if (isOffRoute) {
      navigation.startNavigation(route);
    } else {
      startNavigationFab.show();
    }
  }
}