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 File: BasicDirections.java    From mapbox-java with 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 #2
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #3
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #4
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #5
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #6
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #7
Source File: BasicDirections.java    From mapbox-java with 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 #8
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #9
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #10
Source File: RerouteActivity.java    From graphhopper-navigation-android with 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 #11
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #12
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 #13
Source File: NavigationLauncherActivity.java    From graphhopper-navigation-android with 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 #14
Source File: DirectionsResponseFactory.java    From mapbox-java with 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 #15
Source File: BasicDirections.java    From mapbox-java with 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 #16
Source File: FasterRouteDetectorTest.java    From graphhopper-navigation-android with 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 #17
Source File: BasicDirectionsRefresh.java    From mapbox-java with 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 #18
Source File: FasterRouteDetectorTest.java    From graphhopper-navigation-android with 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 #19
Source File: NavigationEventDispatcherTest.java    From graphhopper-navigation-android with 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 #20
Source File: MapboxDirectionsTest.java    From mapbox-java with 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 File: FasterRouteDetectorTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private DirectionsRoute obtainADirectionsRoute() throws IOException {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(PRECISION_6);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  DirectionsRoute aRoute = response.routes().get(0);
  return aRoute;
}
 
Example #22
Source File: MapboxDirections.java    From mapbox-java with MIT License 5 votes vote down vote up
private Call<DirectionsResponse> get() {
  return getService().getCall(
    ApiCallHelper.getHeaderUserAgent(clientAppName()),
    user(),
    profile(),
    FormatUtils.formatCoordinates(coordinates()),
    accessToken(),
    alternatives(),
    geometries(),
    overview(),
    radius(),
    steps(),
    bearing(),
    continueStraight(),
    annotation(),
    language(),
    roundaboutExits(),
    voiceInstructions(),
    bannerInstructions(),
    voiceUnits(),
    exclude(),
    approaches(),
    waypointIndices(),
    waypointNames(),
    waypointTargets(),
    enableRefresh(),
    walkingSpeed(),
    walkwayBias(),
    alleyBias()
  );
}
 
Example #23
Source File: MapboxDirections.java    From mapbox-java with MIT License 5 votes vote down vote up
private Call<DirectionsResponse> callForUrlLength() {
  Call<DirectionsResponse> get = get();
  if (get.request().url().toString().length() < MAX_URL_SIZE) {
    return get;
  }
  return post();
}
 
Example #24
Source File: MapboxDirections.java    From mapbox-java with MIT License 5 votes vote down vote up
private Call<DirectionsResponse> post() {
  return getService().postCall(
    ApiCallHelper.getHeaderUserAgent(clientAppName()),
    user(),
    profile(),
    FormatUtils.formatCoordinates(coordinates()),
    accessToken(),
    alternatives(),
    geometries(),
    overview(),
    radius(),
    steps(),
    bearing(),
    continueStraight(),
    annotation(),
    language(),
    roundaboutExits(),
    voiceInstructions(),
    bannerInstructions(),
    voiceUnits(),
    exclude(),
    approaches(),
    waypointIndices(),
    waypointNames(),
    waypointTargets(),
    enableRefresh(),
    walkingSpeed(),
    walkwayBias(),
    alleyBias()
  );
}
 
Example #25
Source File: NavigationFasterRouteListenerTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@Test
public void onResponseReceived_slowerRouteIsNotSentToDispatcher() {
  NavigationEventDispatcher eventDispatcher = mock(NavigationEventDispatcher.class);
  FasterRoute fasterRoute = buildFasterRouteThatReturns(false);
  NavigationFasterRouteListener listener = new NavigationFasterRouteListener(eventDispatcher, fasterRoute);
  DirectionsResponse response = buildDirectionsResponse();
  RouteProgress routeProgress = mock(RouteProgress.class);

  listener.onResponseReceived(response, routeProgress);

  verifyZeroInteractions(eventDispatcher);
}
 
Example #26
Source File: NavigationFasterRouteListenerTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@Test
public void onResponseReceived_fasterRouteIsSentToDispatcher() {
  NavigationEventDispatcher eventDispatcher = mock(NavigationEventDispatcher.class);
  FasterRoute fasterRoute = buildFasterRouteThatReturns(true);
  NavigationFasterRouteListener listener = new NavigationFasterRouteListener(eventDispatcher, fasterRoute);
  DirectionsResponse response = buildDirectionsResponse();
  RouteProgress routeProgress = mock(RouteProgress.class);

  listener.onResponseReceived(response, routeProgress);

  verify(eventDispatcher).onFasterRouteEvent(any(DirectionsRoute.class));
}
 
Example #27
Source File: FasterRouteDetectorTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private DirectionsResponse obtainADirectionsResponse() throws IOException {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(PRECISION_6);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  return response;
}
 
Example #28
Source File: NavigationHelperTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private DirectionsRoute buildDistanceCongestionAnnotationRoute() throws IOException {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(ANNOTATED_DISTANCE_CONGESTION_ROUTE_FIXTURE);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  return response.routes().get(0);
}
 
Example #29
Source File: NavigationHelperTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private DirectionsRoute buildMultiLegRoute() throws IOException {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(MULTI_LEG_ROUTE_FIXTURE);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  return response.routes().get(0);
}
 
Example #30
Source File: TriggerTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private RouteProgress buildTriggerRouteProgress() throws Exception {
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
  String body = loadJsonFixture(ROUTE_FIXTURE);
  DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
  DirectionsRoute route = response.routes().get(0);
  int stepDistanceRemaining = (int) route.legs().get(0).steps().get(0).distance();
  int legDistanceRemaining = route.legs().get(0).distance().intValue();
  int routeDistance = route.distance().intValue();
  return buildTestRouteProgress(route, stepDistanceRemaining, legDistanceRemaining,
    routeDistance, 1, 0);
}