Skip to content

Commit

Permalink
add via waypoints support to mapbox directions
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 committed Feb 8, 2019
1 parent 928c7a5 commit ddbb213
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface DirectionsService {
* @param voiceUnits voice units
* @param exclude exclude tolls, motorways or more along your route
* @param approaches which side of the road to approach a waypoint
* @param waypoints which input coordinates should be treated as waypoints
* @param waypointNames custom names for waypoints used for the arrival instruction
* @param waypointTargets list of coordinate pairs for drop-off locations
* @return the {@link DirectionsResponse} in a Call wrapper
Expand Down Expand Up @@ -73,6 +74,7 @@ Call<DirectionsResponse> getCall(
@Query("voice_units") String voiceUnits,
@Query("exclude") String exclude,
@Query("approaches") String approaches,
@Query("waypoints") String waypoints,
@Query("waypoint_names") String waypointNames,
@Query("waypoint_targets") String waypointTargets
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.api.directions.v5;

import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

Expand Down Expand Up @@ -91,6 +92,7 @@ protected Call<DirectionsResponse> initializeCall() {
voiceUnits(),
exclude(),
approaches(),
waypoints(),
waypointNames(),
waypointTargets());
}
Expand Down Expand Up @@ -359,6 +361,9 @@ private static String formatWaypointTargets(Point[] waypointTargets) {
@Nullable
abstract String approaches();

@Nullable
abstract String waypoints();

@Nullable
abstract String waypointNames();

Expand Down Expand Up @@ -419,6 +424,7 @@ public abstract static class Builder {
private Point destination;
private Point origin;
private String[] approaches;
private Integer[] waypoints;
private String[] waypointNames;
private Point[] waypointTargets;

Expand Down Expand Up @@ -787,6 +793,26 @@ public Builder addApproaches(String... approaches) {

abstract Builder approaches(@Nullable String approaches);

/**
* Optionally, set which input coordinates should be treated as waypoints.
* <p>
* Most useful in combination with steps=true and requests based on traces
* with high sample rates. Can be an index corresponding to any of the input coordinates,
* but must contain the first ( 0 ) and last coordinates' index separated by ; .
* {@link #steps()}
* </p>
*
* @param waypoints integer array of coordinate indices to be used as waypoints
* @return this builder for chaining options together
* @since 4.4.0
*/
public Builder addWaypoints(@Nullable @IntRange(from = 0) Integer... waypoints) {
this.waypoints = waypoints;
return this;
}

abstract Builder waypoints(@Nullable String waypoints);

/**
* Custom names for waypoints used for the arrival instruction,
* each separated by ; . Values can be any string and total number of all characters cannot
Expand Down Expand Up @@ -845,6 +871,24 @@ public MapboxDirections build() {
+ " directions API request.");
}

if (waypoints != null) {
if (waypoints.length < 2) {
throw new ServicesException(
"Waypoints must be a list of at least two indexes separated by ';'");
}
if (waypoints[0] != 0 || waypoints[waypoints.length - 1] != coordinates.size() - 1) {
throw new ServicesException(
"Waypoints must contain indices of the first and last coordinates"
);
}
for (int i = 1; i < waypoints.length - 1; i++) {
if (waypoints[i] < 0 || waypoints[i] >= coordinates.size()) {
throw new ServicesException(
"Waypoints index too large (no corresponding coordinate)");
}
}
}

if (waypointNames != null) {
if (waypointNames.length != coordinates.size()) {
throw new ServicesException("Number of waypoint names must match "
Expand Down Expand Up @@ -879,6 +923,7 @@ public MapboxDirections build() {
bearing(TextUtils.formatBearing(bearings));
annotation(TextUtils.join(",", annotations));
radius(TextUtils.formatRadiuses(radiuses));
waypoints(TextUtils.join(";", waypoints));

MapboxDirections directions = autoBuild();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ public static Builder builder() {
@Nullable
public abstract String approaches();

@Nullable
public abstract String waypoints();

/**
* Custom names for waypoints used for the arrival instruction in banners and voice instructions,
* each separated by ; . Values can be any string and total number of all characters cannot
Expand Down Expand Up @@ -541,6 +544,9 @@ public abstract Builder overview(
@Nullable
public abstract Builder approaches(String approaches);

@Nullable
public abstract Builder waypoints(@Nullable String indices);

/**
* The same waypoint names the user originally made when the request was made.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,53 @@ public void testRouteOptionsApproaches() throws Exception {
assertEquals("unrestricted;curb", approaches);
}

@Test(expected = ServicesException.class)
public void build_exceptionThrownWhenLessThanTwoWaypointsProvided() {
MapboxDirections.builder()
.origin(Point.fromLngLat(2.0, 2.0))
.destination(Point.fromLngLat(4.0, 4.0))
.addWaypoints(0)
.baseUrl("https://foobar.com")
.accessToken(ACCESS_TOKEN)
.build();
}

@Test(expected = ServicesException.class)
public void build_exceptionThrownWhenWaypointsDoNotStartWith0() {
MapboxDirections.builder()
.origin(Point.fromLngLat(2.0, 2.0))
.addWaypoint(Point.fromLngLat(3.0, 3.0))
.destination(Point.fromLngLat(4.0, 4.0))
.addWaypoints(1, 2)
.baseUrl("https://foobar.com")
.accessToken(ACCESS_TOKEN)
.build();
}

@Test(expected = ServicesException.class)
public void build_exceptionThrownWhenWaypointDoNotEndWithLast() {
MapboxDirections.builder()
.origin(Point.fromLngLat(2.0, 2.0))
.addWaypoint(Point.fromLngLat(3.0, 3.0))
.destination(Point.fromLngLat(4.0, 4.0))
.addWaypoints(0, 1)
.baseUrl("https://foobar.com")
.accessToken(ACCESS_TOKEN)
.build();
}

@Test(expected = ServicesException.class)
public void build_exceptionThrownWhenMiddleWaypointsAreWrong() {
MapboxDirections.builder()
.origin(Point.fromLngLat(2.0, 2.0))
.addWaypoint(Point.fromLngLat(3.0, 3.0))
.destination(Point.fromLngLat(4.0, 4.0))
.addWaypoints(0, 3, 2)
.baseUrl("https://foobar.com")
.accessToken(ACCESS_TOKEN)
.build();
}

@Test
public void sanityWaypointNamesInstructions() throws Exception {
MapboxDirections mapboxDirections = MapboxDirections.builder()
Expand Down

0 comments on commit ddbb213

Please sign in to comment.