Skip to content

Commit dfd51b5

Browse files
author
Cameron Mace
authored
Cleaned up routeOptions class (#693)
1 parent 113a6b4 commit dfd51b5

6 files changed

Lines changed: 238 additions & 167 deletions

File tree

samples/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
apply plugin: 'java'
22
apply plugin: 'de.fuerstenau.buildconfig'
33

4+
sourceCompatibility = JavaVersion.VERSION_1_8
5+
targetCompatibility = JavaVersion.VERSION_1_8
6+
47
dependencies {
58
compile project(":services-core")
69
compile project(":services-directions")
Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapbox.samples;
22

3+
import com.mapbox.api.directions.v5.DirectionsCriteria;
34
import com.mapbox.api.directions.v5.MapboxDirections;
45
import com.mapbox.api.directions.v5.models.DirectionsResponse;
56
import com.mapbox.geojson.Point;
@@ -13,16 +14,17 @@
1314
/**
1415
* Shows how to make a directions request using some of the parameters offered.
1516
*/
16-
public class BasicDirections implements Callback<DirectionsResponse> {
17+
public class BasicDirections {
1718

1819
public static void main(String[] args) throws IOException {
19-
20-
buildMapboxDirectionsRequest();
21-
22-
20+
simpleMapboxDirectionsRequest();
21+
asyncMapboxDirectionsRequest();
2322
}
2423

25-
private static void buildMapboxDirectionsRequest() throws IOException {
24+
/**
25+
* Demonstrates how to make the most basic directions request.
26+
*/
27+
private static void simpleMapboxDirectionsRequest() throws IOException {
2628

2729
MapboxDirections.Builder builder = MapboxDirections.builder();
2830

@@ -36,68 +38,39 @@ private static void buildMapboxDirectionsRequest() throws IOException {
3638

3739
// 3. Log information from the response
3840
System.out.printf("Check that the response is successful %b", response.isSuccessful());
39-
System.out.printf("Get the first routes distance from origin to destination: %f",
41+
System.out.printf("%nGet the first routes distance from origin to destination: %f",
4042
response.body().routes().get(0).distance());
41-
42-
43-
//
44-
//
45-
//
46-
//
47-
//
48-
// return MapboxDirections.builder()
49-
// .origin(Point.fromLngLat(-95.6332, 29.7890))
50-
// .destination(Point.fromLngLat(-95.3591, 29.7576))
51-
// .bannerInstructions(true)
52-
// .voiceInstructions(true)
53-
// .annotations(DirectionsCriteria.ANNOTATION_CONGESTION)
54-
// .overview(DirectionsCriteria.OVERVIEW_FULL)
55-
// .addBearing(null, null)
56-
// .radiuses(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)
57-
// .steps(true)
58-
// .build();
59-
}
60-
61-
62-
@Override
63-
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
64-
6543
}
6644

67-
@Override
68-
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
69-
45+
/**
46+
* Demonstrates how to make an asynchronous directions request.
47+
*/
48+
private static void asyncMapboxDirectionsRequest() {
49+
50+
// 1. Pass in all the required information to get a route.
51+
MapboxDirections request = MapboxDirections.builder()
52+
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
53+
.origin(Point.fromLngLat(-95.6332, 29.7890))
54+
.destination(Point.fromLngLat(-95.3591, 29.7576))
55+
.profile(DirectionsCriteria.PROFILE_CYCLING)
56+
.steps(true)
57+
.build();
58+
59+
// 2. Now request the route using a async call
60+
request.enqueueCall(new Callback<DirectionsResponse>() {
61+
@Override
62+
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
63+
// 3. Log information from the response
64+
if (response.isSuccessful()) {
65+
System.out.printf("%nGet the street name of the first step along the route: %s",
66+
response.body().routes().get(0).legs().get(0).steps().get(0).name());
67+
}
68+
}
69+
70+
@Override
71+
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
72+
System.err.println(throwable);
73+
}
74+
});
7075
}
71-
}
72-
73-
74-
//
75-
//
76-
//
77-
// // 1. Build the directions request using the provided builder.
78-
// MapboxDirections directions = buildMapboxDirections();
79-
//
80-
//// 2. Use either
81-
// directions.enqueueCall(new Callback<DirectionsResponse>() {
82-
//@Override
83-
//public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
84-
// System.out.println(response.code());
85-
// System.out.println(call.request().url());
86-
// System.out.println(response.body().routes().get(0).legs().get(0).steps().get(0).maneuver().location().latitude());
87-
// System.out.println(response.body().routes().get(0).distance());
88-
// System.out.println(response.body().routes().get(0).routeOptions().profile());
89-
// System.out.println(response.body().routes().get(0).routeOptions().alternatives());
90-
// System.out.println(response.body().routes().get(0).routeOptions().user());
91-
// System.out.println(response.body().routes().get(0).legs().get(0).steps().get(0).maneuver().toString());
92-
// System.out.println(response.body().routes().get(0).legs().get(0).steps().get(0)
93-
// .voiceInstructions().get(0).announcement());
94-
// System.out.println(response.body().routes().get(0).legs().get(0).annotation().congestion().size());
95-
// System.out.println("Distance: " + response.body().routes().get(0).legs().get(0).steps().get(0).bannerInstructions().get(0).distanceAlongGeometry());
96-
// }
97-
//
98-
//@Override
99-
//public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
100-
// System.out.println(call.request().url());
101-
// System.out.println(throwable);
102-
// }
103-
// });
76+
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private Call<DirectionsResponse> getCall() {
101101
ApiCallHelper.getHeaderUserAgent(clientAppName()),
102102
user(),
103103
profile(),
104-
coordinates(),
104+
formatCoordinates(coordinates()),
105105
accessToken(),
106106
alternatives(),
107107
geometries(),
@@ -196,6 +196,7 @@ private List<DirectionsRoute> generateRouteOptions(Response<DirectionsResponse>
196196
modifiedRoutes.add(route.toBuilder().routeOptions(
197197
RouteOptions.builder()
198198
.profile(profile())
199+
.coordinates(coordinates())
199200
.continueStraight(continueStraight())
200201
.annotations(annotation())
201202
.bearings(bearing())
@@ -215,6 +216,17 @@ private List<DirectionsRoute> generateRouteOptions(Response<DirectionsResponse>
215216
return modifiedRoutes;
216217
}
217218

219+
private static String formatCoordinates(List<Point> coordinates) {
220+
List<String> coordinatesFormatted = new ArrayList<>();
221+
for (Point point : coordinates) {
222+
coordinatesFormatted.add(String.format(Locale.US, "%s,%s",
223+
TextUtils.formatCoordinate(point.longitude()),
224+
TextUtils.formatCoordinate(point.latitude())));
225+
}
226+
227+
return TextUtils.join(";", coordinatesFormatted.toArray());
228+
}
229+
218230
/**
219231
* Wrapper method for Retrofits {@link Call#cancel()} call, important to manually cancel call if
220232
* the user dismisses the calling activity or no longer needs the returned results.
@@ -244,7 +256,7 @@ public Call<DirectionsResponse> cloneCall() {
244256
abstract String profile();
245257

246258
@NonNull
247-
abstract String coordinates();
259+
abstract List<Point> coordinates();
248260

249261
@NonNull
250262
abstract String baseUrl();
@@ -696,7 +708,7 @@ public Builder radiuses(@FloatRange(from = 0) double... radiuses) {
696708
*/
697709
public abstract Builder baseUrl(String baseUrl);
698710

699-
abstract Builder coordinates(@NonNull String coordinates);
711+
abstract Builder coordinates(@NonNull List<Point> coordinates);
700712

701713
abstract MapboxDirections autoBuild();
702714

@@ -721,7 +733,7 @@ public MapboxDirections build() {
721733
+ " directions API request.");
722734
}
723735

724-
coordinates(formatCoordinates(coordinates));
736+
coordinates(coordinates);
725737
bearing(TextUtils.formatBearing(bearings));
726738
annotation(TextUtils.join(",", annotations));
727739
radius(TextUtils.formatRadiuses(radiuses));
@@ -734,16 +746,5 @@ public MapboxDirections build() {
734746
}
735747
return directions;
736748
}
737-
738-
private static String formatCoordinates(List<Point> coordinates) {
739-
List<String> coordinatesFormatted = new ArrayList<>();
740-
for (Point point : coordinates) {
741-
coordinatesFormatted.add(String.format(Locale.US, "%s,%s",
742-
TextUtils.formatCoordinate(point.longitude()),
743-
TextUtils.formatCoordinate(point.latitude())));
744-
}
745-
746-
return TextUtils.join(";", coordinatesFormatted.toArray());
747-
}
748749
}
749750
}

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

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.mapbox.api.directions.v5.models;
22

3+
import android.support.annotation.NonNull;
34
import android.support.annotation.Nullable;
45

56
import com.google.auto.value.AutoValue;
67
import com.google.gson.Gson;
78
import com.google.gson.TypeAdapter;
89
import com.mapbox.api.directions.v5.DirectionsCriteria;
10+
import com.mapbox.api.directions.v5.MapboxDirections;
11+
import com.mapbox.geojson.Point;
12+
13+
import java.util.List;
914

1015
/**
1116
* Provides information connected to your request that help when a new directions request is needing
@@ -41,21 +46,34 @@ public static Builder builder() {
4146
* @return string value representing the user
4247
* @since 3.0.0
4348
*/
44-
@Nullable
49+
@NonNull
4550
public abstract String user();
4651

4752
/**
4853
* The same profile which was used during the request that resulted in this root directions
49-
* response.
54+
* response. {@link MapboxDirections#builder()} ensures that a profile is always set even if the
55+
* {@link MapboxDirections} requesting object doesn't specifically set a profile.
5056
*
5157
* @return string value representing the profile
5258
* @since 3.0.0
5359
*/
54-
@Nullable
60+
@NonNull
5561
public abstract String profile();
5662

5763
/**
58-
* The same alternative setting which was used during the request that resulted in this root
64+
* The coordinates used for the routes origin, destination, and optionally, waypoints. Note that
65+
* these coordinates are different than the direction responses {@link DirectionsWaypoint}s in
66+
* that these are the non-snapped coordinates.
67+
*
68+
* @return a list of {@link Point}s which represent the route origin, destination, and optionally,
69+
* waypoints
70+
* @since 3.0.0
71+
*/
72+
@NonNull
73+
public abstract List<Point> coordinates();
74+
75+
/**
76+
* The same alternative setting which were used during the request that resulted in this root
5977
* directions response.
6078
*
6179
* @return boolean object representing the setting for alternatives
@@ -143,12 +161,33 @@ public static Builder builder() {
143161
@Nullable
144162
public abstract Boolean bannerInstructions();
145163

164+
/**
165+
* Whether or not the units used inside the voice instruction's string are in imperial or metric.
166+
*
167+
* @return a string matching either imperial or metric
168+
* @since 3.0.0
169+
*/
146170
@Nullable
147171
public abstract String voiceUnits();
148172

173+
/**
174+
* A valid Mapbox access token used to making the request.
175+
*
176+
* @return a string representing the Mapbox access token
177+
* @since 3.0.0
178+
*/
179+
@NonNull
149180
public abstract String accessToken();
150181

151-
@Nullable
182+
/**
183+
* A universally unique identifier (UUID) for identifying and executing a similar specific route
184+
* in the future. {@link MapboxDirections} always waits for the response object which ensures this
185+
* value will never be null.
186+
*
187+
* @return a string containing the request UUID
188+
* @since 3.0.0
189+
*/
190+
@NonNull
152191
public abstract String requestUuid();
153192

154193
/**
@@ -177,7 +216,7 @@ public abstract static class Builder {
177216
* @return this builder for chaining options together
178217
* @since 3.0.0
179218
*/
180-
public abstract Builder user(String user);
219+
public abstract Builder user(@NonNull String user);
181220

182221
/**
183222
* The directions profile that was used during the request time and resulted in this responses
@@ -188,7 +227,19 @@ public abstract static class Builder {
188227
* @return this builder for chaining options together
189228
* @since 3.0.0
190229
*/
191-
public abstract Builder profile(@Nullable @DirectionsCriteria.ProfileCriteria String profile);
230+
public abstract Builder profile(@NonNull @DirectionsCriteria.ProfileCriteria String profile);
231+
232+
/**
233+
* The coordinates used for the routes origin, destination, and optionally, waypoints. Note that
234+
* these coordinates are different than the direction responses {@link DirectionsWaypoint}s in
235+
* that these are the non-snapped coordinates.
236+
*
237+
* @param coordinates a list of {@link Point}s which represent the route origin, destination,
238+
* and optionally, waypoints
239+
* @return this builder for chaining options together
240+
* @since 3.0.0
241+
*/
242+
public abstract Builder coordinates(@NonNull List<Point> coordinates);
192243

193244
/**
194245
* Whether the alternatives value was set to true or not.
@@ -197,7 +248,7 @@ public abstract static class Builder {
197248
* @return this builder for chaining options together
198249
* @since 3.0.0
199250
*/
200-
public abstract Builder alternatives(Boolean alternatives);
251+
public abstract Builder alternatives(@Nullable Boolean alternatives);
201252

202253
/**
203254
* The language for instructions to be in when the response is given.
@@ -265,11 +316,33 @@ public abstract static class Builder {
265316
*/
266317
public abstract Builder bannerInstructions(Boolean bannerInstructions);
267318

319+
/**
320+
* Whether or not the units used inside the voice instruction's string are in imperial or metric.
321+
*
322+
* @param voiceUnits string matching either imperial or metric
323+
* @return this builder for chaining options together
324+
* @since 3.0.0
325+
*/
268326
public abstract Builder voiceUnits(@Nullable String voiceUnits);
269327

270-
public abstract Builder accessToken(String accessToken);
328+
/**
329+
* A valid Mapbox access token used to making the request.
330+
*
331+
* @param accessToken a string containing a valid Mapbox access token
332+
* @return this builder for chaining options together
333+
* @since 3.0.0
334+
*/
335+
public abstract Builder accessToken(@NonNull String accessToken);
271336

272-
public abstract Builder requestUuid(String requestUuid);
337+
/**
338+
* A universally unique identifier (UUID) for identifying and executing a similar specific route
339+
* in the future.
340+
*
341+
* @param requestUuid a string containing the request UUID
342+
* @return this builder for chaining options together
343+
* @since 3.0.0
344+
*/
345+
public abstract Builder requestUuid(@NonNull String requestUuid);
273346

274347
/**
275348
* The same exclusions the user originally made when the request was made.

0 commit comments

Comments
 (0)