Skip to content

Commit 2026c6e

Browse files
add waypoint targets support into navigation route (#1640)
1 parent a0e2941 commit 2026c6e

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

gradle/dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ext {
99

1010
version = [
1111
mapboxMapSdk : '6.7.2',
12-
mapboxSdkServices : '4.2.0',
12+
mapboxSdkServices : '4.3.0',
1313
mapboxEvents : '3.5.6',
1414
mapboxNavigator : '3.4.11',
1515
searchSdk : '0.1.0-SNAPSHOT',

libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRoute.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public void cancelCall() {
115115
*/
116116
public static final class Builder {
117117

118+
private static final String SEMICOLON = ";";
119+
private static final String COMMA = ",";
118120
private final MapboxDirections.Builder directionsBuilder;
119121

120122
/**
@@ -481,6 +483,26 @@ public Builder addWaypointNames(@Nullable String... waypointNames) {
481483
return this;
482484
}
483485

486+
/**
487+
* A list of coordinate pairs used to specify drop-off
488+
* locations that are distinct from the locations specified in coordinates.
489+
* If this parameter is provided, the Directions API will compute the side of the street,
490+
* <tt>left</tt> or <tt>right</tt>, for each target based on the <tt>waypoint_targets</tt>
491+
* and the driving direction.
492+
* The <tt>maneuver.modifier</tt>, banner and voice instructions will be updated with the computed
493+
* side of street. The number of waypoint targets must be the same as the number of coordinates,
494+
* but you can skip a coordinate pair and show its position in the list adding <tt>null</tt>.
495+
* Must be used with <tt>steps=true</tt>.
496+
*
497+
* @param waypointTargets {@link Point} coordinates for drop-off locations
498+
* @return this builder for chaining options together
499+
* @since 0.26.0
500+
*/
501+
public Builder addWaypointTargets(@Nullable Point... waypointTargets) {
502+
directionsBuilder.addWaypointTargets(waypointTargets);
503+
return this;
504+
}
505+
484506
/**
485507
* Optionally create a {@link Builder} based on all variables
486508
* from given {@link RouteOptions}.
@@ -532,15 +554,21 @@ public Builder routeOptions(RouteOptions options) {
532554
}
533555

534556
if (!TextUtils.isEmpty(options.approaches())) {
535-
String[] approaches = options.approaches().split(";");
557+
String[] approaches = options.approaches().split(SEMICOLON);
536558
directionsBuilder.addApproaches(approaches);
537559
}
538560

539561
if (!TextUtils.isEmpty(options.waypointNames())) {
540-
String[] waypointNames = options.waypointNames().split(";");
562+
String[] waypointNames = options.waypointNames().split(SEMICOLON);
541563
directionsBuilder.addWaypointNames(waypointNames);
542564
}
543565

566+
String waypointTargets = options.waypointTargets();
567+
if (!TextUtils.isEmpty(waypointTargets)) {
568+
Point[] splittedWaypointTargets = parseWaypointTargets(waypointTargets);
569+
directionsBuilder.addWaypointTargets(splittedWaypointTargets);
570+
}
571+
544572
return this;
545573
}
546574

@@ -563,5 +591,23 @@ public NavigationRoute build() {
563591
.roundaboutExits(true);
564592
return new NavigationRoute(directionsBuilder.build());
565593
}
594+
595+
@NonNull
596+
private Point[] parseWaypointTargets(String waypointTargets) {
597+
String[] splittedWaypointTargets = waypointTargets.split(SEMICOLON);
598+
Point[] waypoints = new Point[splittedWaypointTargets.length];
599+
int index = 0;
600+
for (String waypointTarget : splittedWaypointTargets) {
601+
String[] point = waypointTarget.split(COMMA);
602+
if (waypointTarget.isEmpty()) {
603+
waypoints[index++] = null;
604+
} else {
605+
double longitude = Double.valueOf(point[0]);
606+
double latitude = Double.valueOf(point[0]);
607+
waypoints[index++] = Point.fromLngLat(longitude, latitude);
608+
}
609+
}
610+
return waypoints;
611+
}
566612
}
567613
}

libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRouteTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ public void addWaypointNamesIncludedInRequest() {
9595
containsString("Destination"));
9696
}
9797

98+
@Test
99+
public void addWaypointTargetsIncludedInRequest() {
100+
NavigationRoute navigationRoute = NavigationRoute.builder(context, localeUtils)
101+
.accessToken(ACCESS_TOKEN)
102+
.origin(Point.fromLngLat(1.0, 2.0))
103+
.destination(Point.fromLngLat(1.0, 5.0))
104+
.addWaypointTargets(null, Point.fromLngLat(0.99, 4.99))
105+
.build();
106+
107+
assertThat(navigationRoute.getCall().request().url().toString(),
108+
containsString("waypoint_targets"));
109+
}
110+
98111
@Test
99112
public void addingPointAndBearingKeepsCorrectOrder() throws Exception {
100113
NavigationRoute navigationRoute = NavigationRoute.builder(context, localeUtils)
@@ -140,6 +153,7 @@ public void addRouteOptionsIncludedInRequest() throws Exception {
140153
.geometries("mocked_geometries")
141154
.approaches("curb;unrestricted")
142155
.waypointNames("Origin;Destination")
156+
.waypointTargets(";0.99,4.99")
143157
.build();
144158

145159
NavigationRoute navigationRoute = NavigationRoute.builder(context, localeUtils)
@@ -158,6 +172,7 @@ public void addRouteOptionsIncludedInRequest() throws Exception {
158172
assertThat(request, containsString("walking"));
159173
assertThat(request, containsString("curb"));
160174
assertThat(request, containsString("Origin"));
175+
assertThat(request, containsString("waypoint_targets"));
161176
}
162177

163178
@Test

0 commit comments

Comments
 (0)