Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Mapbox welcomes participation and contributions from everyone.

### v2.0.0-beta.2

* Bearing query parameter added to the Directions API

### v2.0.0-beta.1

* Introduce support for Mapbox Distance API
Expand All @@ -16,10 +20,10 @@ Mapbox welcomes participation and contributions from everyone.
* Directions improvements:
* Added direction v5 lanes
* Added support for the `driving-traffic` profile
* Added support for polyline6
* Added support for polyline6
* Remove support for `v4`
* Geocoding improvements:
* Batch requests support
* Batch requests support
* Added landmark, limit, country parameters support
* Added cancel method, setting multiple types, and language method setting to autocomplete widget

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private void getRoute(Position origin, Position destination) throws ServicesExce
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
.setSteps(true)
.setOverview(DirectionsCriteria.OVERVIEW_FULL)
.setBearings(new double[] {60, 45}, new double[] {45, 45})
.build();

MapboxDirectionsRx clientRx = new MapboxDirectionsRx.Builder()
Expand All @@ -135,6 +136,7 @@ public void call(DirectionsResponse response) {
client.enqueueCall(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {

// You can get generic HTTP info about the response
Log.d(LOG_TAG, "Response code: " + response.code());
if (response.body() == null) {
Expand All @@ -161,7 +163,7 @@ public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {

private void drawRoute(DirectionsRoute route) {
// Convert LineString coordinates into LatLng[]
LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.OSRM_PRECISION_V5);
LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6);
List<Position> coordinates = lineString.getCoordinates();
LatLng[] points = new LatLng[coordinates.size()];
for (int i = 0; i < coordinates.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface DirectionsServiceRx {
* @param overview Route full, simplified, etc.
* @param radiuses start at the most efficient point within the radius.
* @param steps Define if you'd like the route steps.
* @param bearings Used to filter the road segment the waypoint will be placed on by direction and dictates
* the angle of approach
* @param continueStraight Define whether the route should continue straight even if the route
* will be slower.
* @return A retrofit Observable object
Expand All @@ -45,6 +47,7 @@ Observable<DirectionsResponse> getObservable(
@Query("overview") String overview,
@Query("radiuses") String radiuses,
@Query("steps") Boolean steps,
@Query("bearings") String bearings,
@Query("continue_straight") Boolean continueStraight
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public Observable<DirectionsResponse> getObservable() {
builder.getOverview(),
builder.getRadiuses(),
builder.isSteps(),
builder.getBearings(),
builder.isContinueStraight());

// Done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public interface DirectionsService {
* @param overview Route full, simplified, etc.
* @param radiuses start at the most efficient point within the radius.
* @param steps Define if you'd like the route steps.
* @param bearings Used to filter the road segment the waypoint will be placed on by direction and dictates
* the angle of approach
* @param continueStraight Define whether the route should continue straight even if the route
* will be slower.
* @return A retrofit Call object
* will be slower. @return A retrofit Call object
* @since 1.0.0
*/
@GET("directions/v5/{user}/{profile}/{coordinates}")
Expand All @@ -46,6 +47,7 @@ Call<DirectionsResponse> getCall(
@Query("overview") String overview,
@Query("radiuses") String radiuses,
@Query("steps") Boolean steps,
@Query("bearings") String bearings,
@Query("continue_straight") Boolean continueStraight
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private Call<DirectionsResponse> getCall() {
builder.getOverview(),
builder.getRadiuses(),
builder.isSteps(),
builder.getBearings(),
builder.isContinueStraight());

// Done
Expand Down Expand Up @@ -136,6 +137,7 @@ public static class Builder<T extends Builder> extends MapboxBuilder {
private String geometries = null;
private String overview = null;
private double[] radiuses = null;
private double[][] bearings = null;
private Boolean steps = null;
private Boolean continueStraight = null;

Expand Down Expand Up @@ -287,6 +289,27 @@ public T setOverview(String overview) {
return (T) this;
}

/**
* Used to filter the road segment the waypoint will be placed on by direction and dictates the angle of approach.
* This option should always be used in conjunction with the {@link MapboxDirections.Builder#setRadiuses(double[])}
* parameter. The parameter takes two values per waypoint: the first is an angle clockwise from true north
* between 0 and 360. The second is the range of degrees the angle can deviate by. We recommend a value of 45
* degrees or 90 degrees for the range, as bearing measurements tend to be inaccurate. This is useful for making
* sure we reroute vehicles on new routes that continue traveling in their current direction. A request that does
* this would provide bearing and radius values for the first waypoint and leave the remaining values empty. If
* provided, the list of bearings must be the same length as the list of waypoints, but you can skip a coordinate
* and show its position by passing in a double array with no values included like so: {@code new double[] {}}.
*
* @param bearings a double array with two values indicating the angle and the other value indicating the deviating
* range.
* @return Builder
* @since 2.0.0
*/
public T setBearings(double[]... bearings) {
this.bearings = bearings;
return (T) this;
}

/**
* Optionally, set a radius values for the coordinates to allow for a more flexible origin
* and destinations point locations.
Expand Down Expand Up @@ -406,6 +429,26 @@ public String getOverview() {
return overview;
}

/**
* @return The optional bearing values if given, otherwise null.
* @since 2.0.0
*/
public String getBearings() {
if (bearings == null || bearings.length == 0) {
return null;
}

String[] bearingFormatted = new String[bearings.length];
for (int i = 0; i < bearings.length; i++) {
if (bearings[i].length == 0) {
bearingFormatted[i] = "";
} else {
bearingFormatted[i] = String.format(Locale.US, "%f,%f", bearings[i][0], bearings[i][1]);
}
}
return TextUtils.join(";", bearingFormatted);
}

/**
* Radiuses indicate how far from a coordinate a routeable way is searched. They
* are indicated like this:
Expand Down Expand Up @@ -500,9 +543,20 @@ public MapboxDirections build() throws ServicesException {
"There must be as many radiuses as there are coordinates.");
}

if (bearings != null) {
for (double[] bearing : bearings) {
if (bearing.length != 2 && bearing.length != 0) {
throw new ServicesException(
"Requesting a route which includes bearings requires exactly 2 values in each double array.");
}
}
}

if (bearings != null && bearings.length != coordinates.size()) {
throw new ServicesException(
"There must be as many bearings as there are coordinates.");
}
return new MapboxDirections(this);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,54 @@ public void testSanity() throws ServicesException, IOException {
assertEquals(response.body().getCode(), DirectionsCriteria.RESPONSE_OK);
}

@Test
public void testBearing() throws ServicesException, IOException {
List<Position> coordinates = new ArrayList<>();
coordinates.add(Position.fromCoordinates(13.4301, 52.5109));
coordinates.add(Position.fromCoordinates(13.4265, 52.5080));
coordinates.add(Position.fromCoordinates(13.4316, 52.5021));

MapboxDirections client = new MapboxDirections.Builder()
.setAccessToken("pk.XXX")
.setCoordinates(coordinates)
.setBearings(new double[] {60, 45}, new double[] {}, new double[] {45, 45})
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
.setBaseUrl(mockUrl.toString())
.setGeometry(DirectionsCriteria.GEOMETRY_POLYLINE)
.build();

assertTrue(client.executeCall().raw().request().url().toString()
.contains("bearings=60.000000,45.000000;;45.000000,45.000000"));
}

@Test
public void testBearingNotEnoughBearingsGiven() throws ServicesException {
thrown.expect(ServicesException.class);
thrown.expectMessage(startsWith("There must be as many bearings as there are coordinates."));

new MapboxDirections.Builder()
.setAccessToken("pk.XXX")
.setCoordinates(positions)
.setBearings(new double[] {})
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
.setBaseUrl(mockUrl.toString())
.build();
}

@Test
public void testBearingArrayLengthNotExactlyTwo() throws ServicesException {
thrown.expect(ServicesException.class);
thrown.expectMessage(startsWith("Requesting a route which includes bearings requires"));

new MapboxDirections.Builder()
.setAccessToken("pk.XXX")
.setCoordinates(positions)
.setBearings(new double[] {0, 0}, new double[] {0})
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
.setBaseUrl(mockUrl.toString())
.build();
}

@Test
public void testDirectionsResponse() throws ServicesException, IOException {
MapboxDirections client = new MapboxDirections.Builder()
Expand Down Expand Up @@ -389,7 +437,6 @@ public void testRotaryLegStepAndStepManeuver() throws ServicesException, IOExcep
assertEquals(step.getPronunciation(), null);
}


@Test
public void testSetCoordinates() {
ArrayList test = new ArrayList<>();
Expand Down