Skip to content

Commit fb6643f

Browse files
committed
Support for v5 directions (#36)
Fixes #31.
1 parent 14d07b7 commit fb6643f

9 files changed

Lines changed: 53 additions & 134 deletions

File tree

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ javadoc:
55
# Output is ./libandroid/lib/build/docs/javadoc/release
66
cd libandroid; ./gradlew clean javadocrelease
77

8-
fixtures:
8+
geocoding-fixtures:
99
# Geocoding: 1600 Pennsylvania Ave NW
10-
curl https://api.mapbox.com/geocoding/v5/mapbox.places/1600+pennsylvania+ave+nw.json?access_token=$(MAPBOX_ACCESS_TOKEN) \
10+
curl "https://api.mapbox.com/geocoding/v5/mapbox.places/1600+pennsylvania+ave+nw.json?access_token=$(MAPBOX_ACCESS_TOKEN)" \
1111
-o libjava/lib/src/test/fixtures/geocoding.json
1212

1313
# Reverse geocoding: -77.0366, 38.8971
14-
curl https://api.mapbox.com/geocoding/v5/mapbox.places/-77.0366,38.8971.json?access_token=$(MAPBOX_ACCESS_TOKEN) \
14+
curl "https://api.mapbox.com/geocoding/v5/mapbox.places/-77.0366,38.8971.json?access_token=$(MAPBOX_ACCESS_TOKEN)" \
1515
-o libjava/lib/src/test/fixtures/geocoding_reverse.json
16+
17+
directions-fixtures:
18+
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)" \
19+
-o libjava/lib/src/test/fixtures/directions_v5.json

libandroid/app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
</activity>
2828
<activity
2929
android:name=".directions.DirectionsV4Activity"
30-
android:label="Directions"
30+
android:label="Directions v4"
31+
android:parentActivityName=".MainActivity"
32+
android:theme="@style/AppTheme.NoActionBar" />
33+
<activity
34+
android:name=".directions.DirectionsV5Activity"
35+
android:label="Directions v5 (experimental)"
3136
android:parentActivityName=".MainActivity"
3237
android:theme="@style/AppTheme.NoActionBar" />
33-
<!-- <activity -->
34-
<!-- android:name=".directions.DirectionsV5Activity" -->
35-
<!-- android:label="Directions v5" -->
36-
<!-- android:theme="@style/AppTheme.NoActionBar" -->
37-
<!-- android:parentActivityName=".MainActivity"/> -->
3838
<activity
3939
android:name=".icons.DirectionsIconsActivity"
40-
android:label="Directions Icons"
40+
android:label="Directions icons"
4141
android:parentActivityName=".MainActivity"
4242
android:theme="@style/AppTheme.NoActionBar" />
4343
<activity
@@ -57,7 +57,7 @@
5757
android:theme="@style/AppTheme.NoActionBar" />
5858
<activity
5959
android:name=".icons.MakiIconsActivity"
60-
android:label="Maki Icons"
60+
android:label="Maki icons"
6161
android:parentActivityName=".MainActivity"
6262
android:theme="@style/AppTheme.NoActionBar" />
6363
<activity

libandroid/app/src/main/java/com/mapbox/services/android/testapp/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public class MainActivity extends AppCompatActivity {
4343
private RecyclerView.LayoutManager mLayoutManager;
4444

4545
private final static List<SampleItem> samples = new ArrayList<>(Arrays.asList(
46-
new SampleItem("Directions", "", DirectionsV4Activity.class),
47-
//new SampleItem("Directions v5", "", DirectionsV5Activity.class),
48-
new SampleItem("Directions Icons", "", DirectionsIconsActivity.class),
46+
new SampleItem("Directions v4", "", DirectionsV4Activity.class),
47+
new SampleItem("Directions v5 (experimental)", "", DirectionsV5Activity.class),
48+
new SampleItem("Directions icons", "", DirectionsIconsActivity.class),
4949
new SampleItem("Reverse geocoding", "", GeocodingReverseActivity.class),
5050
new SampleItem("Geocoding widget", "", GeocodingWidgetActivity.class),
5151
new SampleItem("Geocoding service", "", GeocodingServiceActivity.class),
52-
new SampleItem("Maki Icons", "", MakiIconsActivity.class),
52+
new SampleItem("Maki icons", "", MakiIconsActivity.class),
5353
new SampleItem("Static image", "", StaticImageActivity.class)
5454
));
5555

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ private void getRoute(Position origin, Position destination) throws ServicesExce
110110
positions.add(destination);
111111

112112
MapboxDirections.Builder builder = new MapboxDirections.Builder()
113+
.setAccessToken(Utils.getMapboxAccessToken(this))
113114
.setCoordinates(positions)
114-
.setProfile(DirectionsCriteria.PROFILE_DRIVING);
115+
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
116+
.setSteps(true);
115117

116118
// Check for a custom user
117119
int v5MapboxUser = getResources().getIdentifier("v5_mapbox_user", "string", getPackageName());
@@ -120,15 +122,6 @@ private void getRoute(Position origin, Position destination) throws ServicesExce
120122
builder.setUser(getString(v5MapboxUser));
121123
}
122124

123-
// Check for a custom access token
124-
int v5MapboxAccessToken = getResources().getIdentifier("v5_mapbox_access_token", "string", getPackageName());
125-
if (!TextUtils.isEmpty(getString(v5MapboxAccessToken))) {
126-
Log.d(LOG_TAG, "Custom access token set.");
127-
builder.setAccessToken(getString(v5MapboxAccessToken));
128-
} else {
129-
builder.setAccessToken(Utils.getMapboxAccessToken(this));
130-
}
131-
132125
MapboxDirections client = builder.build();
133126

134127
/*

libjava/lib/src/main/java/com/mapbox/services/commons/models/Bearing.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ Call<DirectionsResponse> getCall(
2020
@Path("coordinates") String coordinates,
2121
@Query("access_token") String accessToken,
2222
@Query("alternative") Boolean alternative,
23-
@Query("bearings") String bearings,
2423
@Query("geometries") String geometries,
2524
@Query("overview") String overview,
2625
@Query("radiuses") String radiuses,
2726
@Query("steps") Boolean steps,
28-
@Query("uturns") Boolean uturns
27+
@Query("continue_straight") Boolean continueStraight
2928
);
3029

3130
@GET("directions/v5/{user}/{profile}/{coordinates}")
@@ -35,12 +34,11 @@ Observable<DirectionsResponse> getObservable(
3534
@Path("coordinates") String coordinates,
3635
@Query("access_token") String accessToken,
3736
@Query("alternative") Boolean alternative,
38-
@Query("bearings") String bearings,
3937
@Query("geometries") String geometries,
4038
@Query("overview") String overview,
4139
@Query("radiuses") String radiuses,
4240
@Query("steps") Boolean steps,
43-
@Query("uturns") Boolean uturns
41+
@Query("continue_straight") Boolean continueStraight
4442
);
4543

4644
}

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

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.mapbox.services.commons.MapboxBuilder;
55
import com.mapbox.services.commons.MapboxService;
66
import com.mapbox.services.commons.ServicesException;
7-
import com.mapbox.services.commons.models.Bearing;
87
import com.mapbox.services.commons.models.Position;
98
import com.mapbox.services.commons.utils.TextUtils;
109
import com.mapbox.services.directions.v5.models.DirectionsResponse;
@@ -73,12 +72,11 @@ private Call<DirectionsResponse> getCall() {
7372
builder.getCoordinates(),
7473
builder.getAccessToken(),
7574
builder.isAlternative(),
76-
builder.getBearings(),
7775
builder.getGeometries(),
7876
builder.getOverview(),
7977
builder.getRadiuses(),
8078
builder.isSteps(),
81-
builder.isUturns());
79+
builder.isContinueStraight());
8280

8381
// Done
8482
return call;
@@ -115,12 +113,11 @@ public Observable<DirectionsResponse> getObservable() {
115113
builder.getCoordinates(),
116114
builder.getAccessToken(),
117115
builder.isAlternative(),
118-
builder.getBearings(),
119116
builder.getGeometries(),
120117
builder.getOverview(),
121118
builder.getRadiuses(),
122119
builder.isSteps(),
123-
builder.isUturns());
120+
builder.isContinueStraight());
124121

125122
// Done
126123
return observable;
@@ -138,12 +135,11 @@ public static class Builder extends MapboxBuilder {
138135
private ArrayList<Position> coordinates = null;
139136
private String accessToken = null;
140137
private Boolean alternative = null;
141-
private Bearing[] bearings = null;
142138
private String geometries = null;
143139
private String overview = null;
144140
private double[] radiuses = null;
145141
private Boolean steps = null;
146-
private Boolean uturns = null;
142+
private Boolean continueStraight = null;
147143

148144
/*
149145
* Constructor
@@ -236,11 +232,6 @@ public Builder setAlternative(Boolean alternative) {
236232
return this;
237233
}
238234

239-
public Builder setBearings(Bearing[] bearings) {
240-
this.bearings = bearings;
241-
return this;
242-
}
243-
244235
public Builder setOverview(String overview) {
245236
this.overview = overview;
246237
return this;
@@ -256,8 +247,8 @@ public Builder setSteps(Boolean steps) {
256247
return this;
257248
}
258249

259-
public Builder setUturns(Boolean uturns) {
260-
this.uturns = uturns;
250+
public Builder setContinueStraight(Boolean continueStraight) {
251+
this.continueStraight = continueStraight;
261252
return this;
262253
}
263254

@@ -303,29 +294,6 @@ public Boolean isAlternative() {
303294
return alternative;
304295
}
305296

306-
/*
307-
* Bearings indicate the allowed direction of travel through a coordinate. They are
308-
* indicated as a query parameter:
309-
*
310-
* ?bearings={direction},{range};{direction},{range}[;{direction},{range} ...]
311-
*
312-
* - Each bearing consists of direction and range, which are separated by a ,
313-
* - There must be as many bearings as there are coordinates
314-
* - It is possible to have empty bearings via ;;, which allow all directions
315-
*/
316-
public String getBearings() {
317-
if (bearings == null || bearings.length == 0) return null;
318-
319-
String[] bearingsFormatted = new String[bearings.length];
320-
for (int i = 0; i < bearings.length; i++) {
321-
bearingsFormatted[i] = String.format(Locale.US, "%d,%d",
322-
bearings[i].getDirection(),
323-
bearings[i].getRange());
324-
}
325-
326-
return TextUtils.join(";", bearingsFormatted);
327-
}
328-
329297
public String getGeometries() {
330298
return geometries;
331299
}
@@ -361,8 +329,8 @@ public Boolean isSteps() {
361329
return steps;
362330
}
363331

364-
public Boolean isUturns() {
365-
return uturns;
332+
public Boolean isContinueStraight() {
333+
return continueStraight;
366334
}
367335

368336
/*
@@ -378,11 +346,6 @@ public MapboxDirections build() throws ServicesException {
378346
"You should provide at least two coordinates (from/to).");
379347
}
380348

381-
if (bearings != null && bearings.length != coordinates.size()) {
382-
throw new ServicesException(
383-
"There must be as many bearings as there are coordinates.");
384-
}
385-
386349
if (radiuses != null && radiuses.length != coordinates.size()) {
387350
throw new ServicesException(
388351
"There must be as many radiuses as there are coordinates.");

libjava/lib/src/test/fixtures/directions_v5.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

libjava/lib/src/test/java/com/mapbox/services/directions/v5/MapboxDirectionsTest.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testSanity() throws ServicesException, IOException {
9393
client.setBaseUrl(mockUrl.toString());
9494
Response<DirectionsResponse> response = client.executeCall();
9595
assertEquals(response.code(), 200);
96-
assertEquals(response.body().getCode(), DirectionsCriteria.RESPONSE_OK.toLowerCase());
96+
assertEquals(response.body().getCode(), DirectionsCriteria.RESPONSE_OK);
9797
}
9898

9999
@Test
@@ -107,7 +107,7 @@ public void testDirectionsResponse() throws ServicesException, IOException {
107107
Response<DirectionsResponse> response = client.executeCall();
108108

109109
DirectionsResponse body = response.body();
110-
assertEquals(body.getCode(), DirectionsCriteria.RESPONSE_OK.toLowerCase());
110+
assertEquals(body.getCode(), DirectionsCriteria.RESPONSE_OK);
111111
assertEquals(body.getRoutes().size(), 1);
112112
assertEquals(body.getWaypoints().size(), 2);
113113
}
@@ -123,9 +123,9 @@ public void testDirectionsRoute() throws ServicesException, IOException {
123123
Response<DirectionsResponse> response = client.executeCall();
124124

125125
DirectionsRoute route = response.body().getRoutes().get(0);
126-
assertEquals(route.getDistance(), 9728.041736092327, DELTA);
127-
assertEquals(route.getDuration(), 1024.3, DELTA);
128-
assertTrue(route.getGeometry().startsWith("orneFh~mjVaENwGPwADGoEI"));
126+
assertEquals(route.getDistance(), 77282.6, DELTA);
127+
assertEquals(route.getDuration(), 2993.1, DELTA);
128+
assertTrue(route.getGeometry().startsWith("kqreFfodjVhh"));
129129
assertEquals(route.getLegs().size(), 1);
130130
}
131131

@@ -140,9 +140,9 @@ public void testDirectionsWaypoint() throws ServicesException, IOException {
140140
Response<DirectionsResponse> response = client.executeCall();
141141

142142
DirectionsWaypoint waypoint = response.body().getWaypoints().get(0);
143-
assertEquals(waypoint.getName(), "8th Ave");
144-
assertEquals(waypoint.asPosition().getLongitude(), -122.465173, DELTA);
145-
assertEquals(waypoint.asPosition().getLatitude(), 37.76312, DELTA);
143+
assertEquals(waypoint.getName(), "Eddy St");
144+
assertEquals(waypoint.asPosition().getLongitude(), -122.416684, DELTA);
145+
assertEquals(waypoint.asPosition().getLatitude(), 37.783415, DELTA);
146146
}
147147

148148
@Test
@@ -156,10 +156,10 @@ public void testRouteLeg() throws ServicesException, IOException {
156156
Response<DirectionsResponse> response = client.executeCall();
157157

158158
RouteLeg leg = response.body().getRoutes().get(0).getLegs().get(0);
159-
assertEquals(leg.getDistance(), 9728.041736092327, DELTA);
160-
assertEquals(leg.getDuration(), 1024.3, DELTA);
161-
assertEquals(leg.getSummary(), "Golden Gate Ave, Leavenworth St");
162-
assertEquals(leg.getSteps().size(), 14);
159+
assertEquals(leg.getDistance(), 77282.6, DELTA);
160+
assertEquals(leg.getDuration(), 2993.1, DELTA);
161+
//assertEquals(leg.getSummary(), "Golden Gate Ave, Leavenworth St");
162+
assertEquals(leg.getSteps().size(), 63);
163163
}
164164

165165
@Test
@@ -173,10 +173,10 @@ public void testLegStep() throws ServicesException, IOException {
173173
Response<DirectionsResponse> response = client.executeCall();
174174

175175
LegStep step = response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(0);
176-
assertEquals(step.getDistance(), 312.82453411511926, DELTA);
177-
assertEquals(step.getDuration(), 44.599999999999994, DELTA);
178-
assertEquals(step.getGeometry(), "orneFh~mjVaENwGPwAD");
179-
assertEquals(step.getName(), "8th Ave");
176+
assertEquals(step.getDistance(), 221.7, DELTA);
177+
assertEquals(step.getDuration(), 48.5, DELTA);
178+
assertEquals(step.getGeometry(), "kqreFfodjVJtAJtAf@fI");
179+
assertEquals(step.getName(), "Eddy St");
180180
assertEquals(step.getMode(), "driving");
181181
assertNotEquals(step.getManeuver(), null);
182182
}
@@ -192,13 +192,13 @@ public void testStepManeuver() throws ServicesException, IOException {
192192
Response<DirectionsResponse> response = client.executeCall();
193193

194194
StepManeuver maneuver = response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(0).getManeuver();
195-
assertEquals(maneuver.asPosition().getLongitude(), -122.465173, DELTA);
196-
assertEquals(maneuver.asPosition().getLatitude(), 37.76312, DELTA);
197-
assertEquals(maneuver.getBearingBefore(), 316.5266871733792, DELTA);
198-
assertEquals(maneuver.getBearingAfter(), 0, DELTA);
195+
assertEquals(maneuver.asPosition().getLongitude(), -122.416684, DELTA);
196+
assertEquals(maneuver.asPosition().getLatitude(), 37.783415, DELTA);
197+
assertEquals(maneuver.getBearingBefore(), 0, DELTA);
198+
assertEquals(maneuver.getBearingAfter(), 260, DELTA);
199199
assertEquals(maneuver.getType(), "depart");
200200
assertEquals(maneuver.getModifier(), "left");
201-
assertEquals(maneuver.getInstruction(), "Head left on 8th Ave towards Lincoln Way");
201+
assertEquals(maneuver.getInstruction(), "Head left on Eddy St");
202202
assertEquals(maneuver.getExit(), 1);
203203
}
204204

0 commit comments

Comments
 (0)