Skip to content

Commit dd543c0

Browse files
author
Łukasz Paczos
committed
refactor RouteOptions and make the a MapboxDirections parameter, fix smaller issues
- refactors `MapboxDirections` to accept `RouteOptions` as an argument that defines request parameters - moves `requestUuid` object out of `RouteOptions` and into `DirectionsRoute` since the UUID is not a request parameter, it's a response value - adds `enableRefresh` to `RouteOptions` since it is a route request parameter - removes a separate `WalkingOptions` in favor of storing the parameter directly in the `RouteOptions` - exposes `arriveBy` and `departAt` arguemnts, superseding #1187 - exposes `ANNOTATION_CONGESTION_NUMERIC` parameter - updates documentation for various route request parameters - exposes `DirectionsResponse#fromJson(json, route options, request uuid)` that allows developers to provide information about the original route request which response they are deserialzing so that Nav SDK can operate correctly - exposes `DirectionsRoute#fromJson(json, route options, request uuid)` for same reason as above - exposes `RouteOptions#fromUrl` utility function that creates the object from the request URL which might be useful when paired with the above functions - adds default values for `RouteOptions`: base URL, user, and geometry encoding (polyline6) - all comma or semicolon separated lists had a `List` overload in `RouteOptions` besides `coordinates, now this is aligned and there is `RouteOptions#coordinates(string)` and `RouteOptions#coordinatesList(list)` - fixes an issue where coordinates were rounded to 6 decimal places instead of 7 decimal places that the GeoJSON specification expects
1 parent 59e274d commit dd543c0

33 files changed

Lines changed: 2861 additions & 3742 deletions

File tree

config/checkstyle/checkstyle.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,6 @@
438438
<property name="scope" value="public"/>
439439
</module>
440440

441-
<!-- Require the @since tag to be on all public javadoc methods. -->
442-
<module name="WriteTag">
443-
<property name="tag" value="@since"/>
444-
<property name="severity" value="warning"/>
445-
<property name="tagSeverity" value="ignore"/>
446-
</module>
447-
448441
<!-- Forbid pointless Javadoc summary sentences. -->
449442
<module name="SummaryJavadocCheck">
450443
<property name="forbiddenSummaryFragments"

samples/src/main/java/com/mapbox/samples/BasicDirections.java

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.mapbox.samples;
22

33
import com.mapbox.api.directions.v5.DirectionsCriteria;
4-
import com.mapbox.api.directions.v5.WalkingOptions;
54
import com.mapbox.api.directions.v5.MapboxDirections;
65
import com.mapbox.api.directions.v5.models.DirectionsResponse;
6+
import com.mapbox.api.directions.v5.models.RouteOptions;
77
import com.mapbox.geojson.Point;
88
import com.mapbox.sample.BuildConfig;
9+
910
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.List;
1013

1114
import retrofit2.Call;
1215
import retrofit2.Callback;
@@ -30,13 +33,17 @@ public static void main(String[] args) throws IOException {
3033
* @throws IOException signals that an I/O exception of some sort has occurred
3134
*/
3235
private static void simpleMapboxDirectionsRequest() throws IOException {
33-
3436
MapboxDirections.Builder builder = MapboxDirections.builder();
3537

3638
// 1. Pass in all the required information to get a simple directions route.
37-
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
38-
builder.origin(Point.fromLngLat(-95.6332, 29.7890));
39-
builder.destination(Point.fromLngLat(-95.3591, 29.7576));
39+
List<Point> coordinates = new ArrayList<>();
40+
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
41+
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
42+
RouteOptions routeOptions = RouteOptions.builder()
43+
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
44+
.coordinatesList(coordinates)
45+
.build();
46+
builder.routeOptions(routeOptions);
4047

4148
// 2. That's it! Now execute the command and get the response.
4249
Response<DirectionsResponse> response = builder.build().executeCall();
@@ -53,19 +60,20 @@ private static void simpleMapboxDirectionsRequest() throws IOException {
5360
* @throws IOException signals that an I/O exception of some sort has occurred
5461
*/
5562
private static void simpleMapboxDirectionsWalkingRequest() throws IOException {
56-
5763
MapboxDirections.Builder builder = MapboxDirections.builder();
5864

59-
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
60-
builder.origin(Point.fromLngLat(-95.6332, 29.7890));
61-
builder.destination(Point.fromLngLat(-95.3591, 29.7576));
62-
builder.profile("walking");
63-
builder.walkingOptions(
64-
WalkingOptions.builder()
65-
.walkingSpeed(1.0)
66-
.walkwayBias(0.6)
67-
.alleyBias(0.7)
68-
.build());
65+
List<Point> coordinates = new ArrayList<>();
66+
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
67+
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
68+
RouteOptions routeOptions = RouteOptions.builder()
69+
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
70+
.coordinatesList(coordinates)
71+
.profile("walking")
72+
.walkingSpeed(1.0)
73+
.walkwayBias(0.6)
74+
.alleyBias(0.7)
75+
.build();
76+
builder.routeOptions(routeOptions);
6977

7078
// 2. That's it! Now execute the command and get the response.
7179
Response<DirectionsResponse> response = builder.build().executeCall();
@@ -84,11 +92,15 @@ private static void simpleMapboxDirectionsWalkingRequest() throws IOException {
8492
private static void simpleMapboxDirectionsPostRequest() throws IOException {
8593

8694
MapboxDirections.Builder builder = MapboxDirections.builder();
87-
88-
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
89-
builder.origin(Point.fromLngLat(-95.6332, 29.7890));
90-
builder.destination(Point.fromLngLat(-95.3591, 29.7576));
91-
builder.post();
95+
List<Point> coordinates = new ArrayList<>();
96+
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
97+
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
98+
RouteOptions routeOptions = RouteOptions.builder()
99+
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
100+
.coordinatesList(coordinates)
101+
.build();
102+
builder.routeOptions(routeOptions);
103+
builder.usePostMethod(true);
92104

93105
Response<DirectionsResponse> response = builder.build().executeCall();
94106

@@ -101,18 +113,22 @@ private static void simpleMapboxDirectionsPostRequest() throws IOException {
101113
* Demonstrates how to make an asynchronous directions request.
102114
*/
103115
private static void asyncMapboxDirectionsRequest() {
116+
MapboxDirections.Builder builder = MapboxDirections.builder();
104117

105118
// 1. Pass in all the required information to get a route.
106-
MapboxDirections request = MapboxDirections.builder()
119+
List<Point> coordinates = new ArrayList<>();
120+
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
121+
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
122+
RouteOptions routeOptions = RouteOptions.builder()
107123
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
108-
.origin(Point.fromLngLat(-95.6332, 29.7890))
109-
.destination(Point.fromLngLat(-95.3591, 29.7576))
124+
.coordinatesList(coordinates)
110125
.profile(DirectionsCriteria.PROFILE_CYCLING)
111126
.steps(true)
112127
.build();
128+
builder.routeOptions(routeOptions);
113129

114130
// 2. Now request the route using a async call
115-
request.enqueueCall(new Callback<DirectionsResponse>() {
131+
builder.build().enqueueCall(new Callback<DirectionsResponse>() {
116132
@Override
117133
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
118134
// 3. Log information from the response

samples/src/main/java/com/mapbox/samples/BasicDirectionsRefresh.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import com.mapbox.api.directions.v5.MapboxDirections;
44
import com.mapbox.api.directions.v5.models.DirectionsResponse;
5+
import com.mapbox.api.directions.v5.models.RouteOptions;
56
import com.mapbox.api.directionsrefresh.v1.MapboxDirectionsRefresh;
67
import com.mapbox.api.directionsrefresh.v1.models.DirectionsRefreshResponse;
78
import com.mapbox.geojson.Point;
89
import com.mapbox.sample.BuildConfig;
910

1011
import java.io.IOException;
11-
12+
import java.util.ArrayList;
1213
import java.util.Arrays;
14+
import java.util.List;
15+
1316
import retrofit2.Call;
1417
import retrofit2.Callback;
1518
import retrofit2.Response;
@@ -31,26 +34,31 @@ public static void main(String[] args) throws IOException {
3134
private static String simpleMapboxDirectionsRequest(MapboxDirections directions) throws IOException {
3235
Response<DirectionsResponse> response = directions.executeCall();
3336
System.out.println("Directions response: " + response);
34-
String requestId = response.body().routes().get(0).routeOptions().requestUuid();
37+
String requestId = response.body().routes().get(0).requestUuid();
3538

3639
return requestId;
3740
}
3841

3942
private static MapboxDirections mapboxDirections(Boolean addWaypoint) {
40-
MapboxDirections.Builder directions = MapboxDirections.builder()
43+
MapboxDirections.Builder builder = MapboxDirections.builder();
44+
45+
List<Point> coordinates = new ArrayList<>();
46+
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
47+
if (addWaypoint) {
48+
coordinates.add(Point.fromLngLat(-95.5591, 29.7376));
49+
}
50+
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
51+
RouteOptions routeOptions = RouteOptions.builder()
4152
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
53+
.coordinatesList(coordinates)
4254
.enableRefresh(true)
43-
.origin(Point.fromLngLat(-95.6332, 29.7890))
44-
.destination(Point.fromLngLat(-95.3591, 29.7576))
4555
.overview(OVERVIEW_FULL)
4656
.profile(PROFILE_DRIVING_TRAFFIC)
47-
.annotations(Arrays.asList(ANNOTATION_CONGESTION,ANNOTATION_MAXSPEED));
48-
49-
if (addWaypoint) {
50-
directions.addWaypoint(Point.fromLngLat(-95.5591, 29.7376));
51-
}
57+
.annotationsList(Arrays.asList(ANNOTATION_CONGESTION, ANNOTATION_MAXSPEED))
58+
.build();
59+
builder.routeOptions(routeOptions);
5260

53-
return directions.build();
61+
return builder.build();
5462
}
5563

5664
private static void simpleMapboxDirectionsRefreshRequest(String requestId, Integer legIndex) {

services-core/src/main/java/com/mapbox/core/MapboxService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
* @since 1.0.0
2222
*/
2323
public abstract class MapboxService<T, S> {
24-
protected static final int MAX_URL_SIZE = 1024 * 8;
24+
25+
/**
26+
* If the url length exceeds this value, the POST method is chosen by default.
27+
*/
28+
public static final int MAX_URL_SIZE = 1024 * 8;
29+
2530
private final Class<S> serviceType;
2631
private boolean enableDebug;
2732
protected OkHttpClient okHttpClient;

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public final class DirectionsCriteria {
1919
*/
2020
public static final String PROFILE_DEFAULT_USER = "mapbox";
2121

22+
/**
23+
* Base URL for all API calls.
24+
*/
25+
public static final String BASE_API_URL = "https://api.mapbox.com";
26+
2227
/**
2328
* For car and motorcycle routing. This profile factors in current and historic traffic
2429
* conditions to avoid slowdowns.
@@ -88,42 +93,53 @@ public final class DirectionsCriteria {
8893
public static final String OVERVIEW_FALSE = "false";
8994

9095
/**
91-
* The duration, in seconds, between each pair of coordinates.
96+
* The duration between each pair of coordinates in seconds.
9297
*
9398
* @since 2.1.0
9499
*/
95100
public static final String ANNOTATION_DURATION = "duration";
96101

97102
/**
98-
* The distance, in meters, between each pair of coordinates.
103+
* The distance between each pair of coordinates in meters.
99104
*
100105
* @since 2.1.0
101106
*/
102107
public static final String ANNOTATION_DISTANCE = "distance";
103108

104109
/**
105-
* The speed, in km/h, between each pair of coordinates.
110+
* The speed between each pair of coordinates in meters per second.
106111
*
107112
* @since 2.1.0
108113
*/
109114
public static final String ANNOTATION_SPEED = "speed";
110115

111116
/**
112-
* The congestion, provided as a String, between each pair of coordinates.
113-
*
114-
* @since 2.2.0
117+
* The level of congestion between each entry in the array of coordinate pairs
118+
* in the route leg.
119+
* This annotation is only available for the {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}.
115120
*/
116121
public static final String ANNOTATION_CONGESTION = "congestion";
117122

118123
/**
119-
* The posted speed limit, between each pair of coordinates.
120-
*
121-
* @since 2.1.0
124+
* The numeric level of congestion between each entry in the array of coordinate pairs
125+
* in the route leg.
126+
* This annotation is only available for the {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}.
127+
*/
128+
public static final String ANNOTATION_CONGESTION_NUMERIC = "congestion_NUMERIC";
129+
130+
/**
131+
* The maximum speed limit between the coordinates of a segment.
132+
* This annotation is only available for
133+
* the {@link DirectionsCriteria#PROFILE_DRIVING} and
134+
* the {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}.
122135
*/
123136
public static final String ANNOTATION_MAXSPEED = "maxspeed";
124137

125138
/**
126-
* The closure of sections of a route.
139+
* An array of closure objects describing live-traffic related closures
140+
* that occur along the route.
141+
* This annotation is only available for
142+
* the {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}.
127143
*/
128144
public static final String ANNOTATION_CLOSURE = "closure";
129145

@@ -213,7 +229,7 @@ public final class DirectionsCriteria {
213229

214230
/**
215231
* The routes can approach waypoints from either side of the road. <p>
216-
*
232+
* <p>
217233
* Used in MapMatching and Directions API.
218234
*
219235
* @since 3.2.0
@@ -222,10 +238,8 @@ public final class DirectionsCriteria {
222238

223239
/**
224240
* The route will be returned so that on arrival,
225-
* the waypoint will be found on the side that corresponds with the driving_side of
226-
* the region in which the returned route is located. <p>
227-
*
228-
* Used in MapMatching and Directions API.
241+
* the waypoint will be found on the side that corresponds with the driving_side of
242+
* the region in which the returned route is located.
229243
*
230244
* @since 3.2.0
231245
*/
@@ -284,11 +298,13 @@ private DirectionsCriteria() {
284298
*/
285299
@Retention(RetentionPolicy.CLASS)
286300
@StringDef( {
287-
ANNOTATION_CONGESTION,
288-
ANNOTATION_DISTANCE,
289301
ANNOTATION_DURATION,
302+
ANNOTATION_DISTANCE,
290303
ANNOTATION_SPEED,
291-
ANNOTATION_MAXSPEED
304+
ANNOTATION_CONGESTION,
305+
ANNOTATION_CONGESTION_NUMERIC,
306+
ANNOTATION_MAXSPEED,
307+
ANNOTATION_CLOSURE
292308
})
293309
public @interface AnnotationCriteria {
294310
}

0 commit comments

Comments
 (0)