Skip to content

Commit 9ce67ca

Browse files
author
Cameron Mace
authored
add support for direction annotations (#417)
1 parent 0285600 commit 9ce67ca

12 files changed

Lines changed: 6022 additions & 4 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ directions-fixtures:
9292
curl "https://api.mapbox.com/directions/v5/mapbox/driving/-122.416667,37.783333;-121.900000,37.333333?geometries=polyline&steps=true&access_token=$(MAPBOX_ACCESS_TOKEN)" \
9393
-o mapbox/libjava-services/src/test/fixtures/directions_v5.json
9494

95+
# Directions: request annotations
96+
curl "https://api.mapbox.com/directions/v5/mapbox/driving/-122.416667,37.783333;-121.900000,37.333333?geometries=polyline&steps=true&annotations=distance,duration,speed&access_token=$(MAPBOX_ACCESS_TOKEN)" \
97+
-o mapbox/libjava-services/src/test/fixtures/directions_annotations_v5.json
98+
9599
# Directions: polyline geometry with precision 6
96100
curl "https://api.mapbox.com/directions/v5/mapbox/driving/-122.416667,37.783333;-121.900000,37.333333?geometries=polyline6&steps=true&access_token=$(MAPBOX_ACCESS_TOKEN)" \
97101
-o mapbox/libjava-services/src/test/fixtures/directions_v5_precision_6.json

mapbox/app/src/main/java/com/mapbox/services/android/testapp/directions/DirectionsV5Activity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private void getRoute(Position origin, Position destination) {
108108
.setSteps(true)
109109
.setOverview(DirectionsCriteria.OVERVIEW_FULL)
110110
.setBearings(new double[] {60, 45}, new double[] {45, 45})
111+
.setAnnotation(DirectionsCriteria.ANNOTATION_DISTANCE, DirectionsCriteria.ANNOTATION_DURATION)
111112
.build();
112113

113114
MapboxDirectionsRx clientRx = new MapboxDirectionsRx.Builder()
@@ -132,6 +133,7 @@ public void accept(DirectionsResponse response) throws Exception {
132133
client.enqueueCall(new Callback<DirectionsResponse>() {
133134
@Override
134135
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
136+
Log.d(LOG_TAG, "API call URL: " + call.request().url().toString());
135137

136138
// You can get generic HTTP info about the response
137139
Log.d(LOG_TAG, "Response code: " + response.code());

mapbox/libjava-services-rx/src/main/java/com/mapbox/services/api/rx/directions/v5/DirectionsServiceRx.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public interface DirectionsServiceRx {
3232
* the angle of approach
3333
* @param continueStraight Define whether the route should continue straight even if the route
3434
* will be slower.
35+
* @param annotations An annotations object that contains additional details about each line segment along the
36+
* route geometry. Each entry in an annotations field corresponds to a coordinate along the
37+
* route geometry.
3538
* @return A retrofit Observable object
3639
* @since 2.0.0
3740
*/
@@ -48,6 +51,7 @@ Observable<DirectionsResponse> getObservable(
4851
@Query("radiuses") String radiuses,
4952
@Query("steps") Boolean steps,
5053
@Query("bearings") String bearings,
51-
@Query("continue_straight") Boolean continueStraight
54+
@Query("continue_straight") Boolean continueStraight,
55+
@Query("annotations") String annotations
5256
);
5357
}

mapbox/libjava-services-rx/src/main/java/com/mapbox/services/api/rx/directions/v5/MapboxDirectionsRx.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public Observable<DirectionsResponse> getObservable() {
6464
builder.getRadiuses(),
6565
builder.isSteps(),
6666
builder.getBearings(),
67-
builder.isContinueStraight());
67+
builder.isContinueStraight(),
68+
builder.getAnnotation());
6869

6970
// Done
7071
return observable;

mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/DirectionsCriteria.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ public class DirectionsCriteria {
8383
*/
8484
public static final String OVERVIEW_FALSE = "false";
8585

86+
/**
87+
* The duration, in seconds, between each pair of coordinates.
88+
*
89+
* @since 2.1.0
90+
*/
91+
public static final String ANNOTATION_DURATION = "duration";
92+
93+
/**
94+
* The distance, in meters, between each pair of coordinates.
95+
*
96+
* @since 2.1.0
97+
*/
98+
public static final String ANNOTATION_DISTANCE = "distance";
99+
100+
/**
101+
* The speed, in km/h, between each pair of coordinates.
102+
*
103+
* @since 2.1.0
104+
*/
105+
public static final String ANNOTATION_SPEED = "speed";
106+
86107
/**
87108
* Server responds with no errors.
88109
*

mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/DirectionsService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public interface DirectionsService {
3232
* the angle of approach
3333
* @param continueStraight Define whether the route should continue straight even if the route
3434
* will be slower. @return A retrofit Call object
35+
* @param annotations An annotations object that contains additional details about each line segment along the
36+
* route geometry. Each entry in an annotations field corresponds to a coordinate along the
37+
* route geometry.
3538
* @since 1.0.0
3639
*/
3740
@GET("directions/v5/{user}/{profile}/{coordinates}")
@@ -48,6 +51,7 @@ Call<DirectionsResponse> getCall(
4851
@Query("radiuses") String radiuses,
4952
@Query("steps") Boolean steps,
5053
@Query("bearings") String bearings,
51-
@Query("continue_straight") Boolean continueStraight
54+
@Query("continue_straight") Boolean continueStraight,
55+
@Query("annotations") String annotations
5256
);
5357
}

mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/MapboxDirections.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ private Call<DirectionsResponse> getCall() {
7474
builder.getRadiuses(),
7575
builder.isSteps(),
7676
builder.getBearings(),
77-
builder.isContinueStraight());
77+
builder.isContinueStraight(),
78+
builder.getAnnotation());
7879

7980
// Done
8081
return call;
@@ -145,6 +146,7 @@ public static class Builder<T extends Builder> extends MapboxBuilder {
145146
private Boolean continueStraight = null;
146147
private Position origin = null;
147148
private Position destination = null;
149+
private String[] annotation = null;
148150

149151
/**
150152
* Constructor
@@ -340,6 +342,22 @@ public T setContinueStraight(Boolean continueStraight) {
340342
return (T) this;
341343
}
342344

345+
/**
346+
* Whether or not to return additional metadata along the route. Possible values are:
347+
* {@link DirectionsCriteria#ANNOTATION_DISTANCE}, {@link DirectionsCriteria#ANNOTATION_DURATION}, and
348+
* {@link DirectionsCriteria#ANNOTATION_SPEED}. Several annotation can be used by separating them with {@code ,}.
349+
*
350+
* @param annotation String referencing one of the annotation direction criteria's.
351+
* @return Builder
352+
* @see <a href="https://www.mapbox.com/api-documentation/#routeleg-object">RouteLeg object documentation</a> for
353+
* more information.
354+
* @since 2.1.0
355+
*/
356+
public T setAnnotation(String... annotation) {
357+
this.annotation = annotation;
358+
return (T) this;
359+
}
360+
343361
/*
344362
* Getters, they return the value in a format ready for the API to consume
345363
*/
@@ -506,6 +524,20 @@ public Boolean isContinueStraight() {
506524
return continueStraight;
507525
}
508526

527+
/**
528+
* returns one or a combination of {@link DirectionsCriteria#ANNOTATION_DISTANCE},
529+
* {@link DirectionsCriteria#ANNOTATION_DURATION}, or {@link DirectionsCriteria#ANNOTATION_SPEED}.
530+
*
531+
* @return String with 1 or several annotations that have been set.
532+
* @since 2.1.0
533+
*/
534+
public String getAnnotation() {
535+
if (annotation == null || annotation.length == 0) {
536+
return null;
537+
}
538+
return TextUtils.join(",", annotation);
539+
}
540+
509541
public T setClientAppName(String appName) {
510542
super.clientAppName = appName;
511543
return (T) this;
@@ -576,6 +608,24 @@ public MapboxDirections build() throws ServicesException {
576608
throw new ServicesException(
577609
"There must be as many bearings as there are coordinates.");
578610
}
611+
612+
if (annotation != null) {
613+
if (annotation.length > 3) {
614+
throw new ServicesException(
615+
"Annotation request can only contain one of the three DirectionsCriteria constants.");
616+
}
617+
618+
// Check that user isn't using incorrect annotation request.
619+
for (String annotationEntry : annotation) {
620+
if (!annotationEntry.equals(DirectionsCriteria.ANNOTATION_DISTANCE)
621+
&& !annotationEntry.equals(DirectionsCriteria.ANNOTATION_DURATION)
622+
&& !annotationEntry.equals(DirectionsCriteria.ANNOTATION_SPEED)) {
623+
throw new ServicesException(
624+
"Annotation value must be one of the constant values found inside DirectionsCriteria");
625+
}
626+
}
627+
}
628+
579629
return new MapboxDirections(this);
580630
}
581631
}

mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/IntersectionLanes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/**
44
* Object representing lanes in an intersection.
5+
*
6+
* @since 2.0.0
57
*/
68
public class IntersectionLanes {
79

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.mapbox.services.api.directions.v5.models;
2+
3+
/**
4+
* An annotations object that contains additional details about each line segment along the route geometry. Each entry
5+
* in an annotations field corresponds to a coordinate along the route geometry.
6+
*
7+
* @since 2.1.0
8+
*/
9+
public class LegAnnotation {
10+
11+
private double[] distance;
12+
private double[] duration;
13+
private double[] speed;
14+
15+
public LegAnnotation() {
16+
}
17+
18+
/**
19+
* The distance, in meters, between each pair of coordinates.
20+
*
21+
* @return a double array with each entry being a distance value between two of the routeLeg geometry coordinates.
22+
* @since 2.1.0
23+
*/
24+
public double[] getDistance() {
25+
return distance;
26+
}
27+
28+
/**
29+
* The duration, in seconds, between each pair of coordinates.
30+
*
31+
* @return a double array with each entry being a duration value between two of the routeLeg geometry coordinates.
32+
* @since 2.1.0
33+
*/
34+
public double[] getDuration() {
35+
return duration;
36+
}
37+
38+
/**
39+
* The speed, in km/h, between each pair of coordinates.
40+
*
41+
* @return a double array with each entry being a speed value between two of the routeLeg geometry coordinates.
42+
* @since 2.1.0
43+
*/
44+
public double[] getSpeed() {
45+
return speed;
46+
}
47+
}

mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/RouteLeg.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class RouteLeg {
1313
private double duration;
1414
private String summary;
1515
private List<LegStep> steps;
16+
private LegAnnotation annotation;
1617

1718
public RouteLeg() {
1819
}
@@ -56,4 +57,15 @@ public String getSummary() {
5657
public List<LegStep> getSteps() {
5758
return steps;
5859
}
60+
61+
/**
62+
* An annotations object that contains additional details about each line segment along the route geometry. If you'd l
63+
* ike to receiving this, you must request it inside your Directions request before executing the call.
64+
*
65+
* @return An {@link LegAnnotation} object.
66+
* @since 2.1.0
67+
*/
68+
public LegAnnotation getAnnotation() {
69+
return annotation;
70+
}
5971
}

0 commit comments

Comments
 (0)