-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathBasicDirections.java
More file actions
147 lines (126 loc) · 5.55 KB
/
Copy pathBasicDirections.java
File metadata and controls
147 lines (126 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.mapbox.samples;
import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.MapboxDirections;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.RouteOptions;
import com.mapbox.geojson.Point;
import com.mapbox.sample.BuildConfig;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Shows how to make a directions request using some of the parameters offered.
*/
public class BasicDirections {
public static void main(String[] args) throws IOException {
simpleMapboxDirectionsRequest();
simpleMapboxDirectionsWalkingRequest();
asyncMapboxDirectionsRequest();
simpleMapboxDirectionsPostRequest();
}
/**
* Demonstrates how to make the most basic GET directions request.
*
* @throws IOException signals that an I/O exception of some sort has occurred
*/
private static void simpleMapboxDirectionsRequest() throws IOException {
MapboxDirections.Builder builder = MapboxDirections.builder();
// 1. Pass in all the required information to get a simple directions route.
List<Point> coordinates = new ArrayList<>();
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
RouteOptions routeOptions = RouteOptions.builder()
.coordinatesList(coordinates)
.build();
builder.routeOptions(routeOptions);
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
// 2. That's it! Now execute the command and get the response.
Response<DirectionsResponse> response = builder.build().executeCall();
// 3. Log information from the response
System.out.printf("Check that the GET response is successful %b", response.isSuccessful());
System.out.printf("%nGet the first routes distance from origin to destination: %f",
response.body().routes().get(0).distance());
}
/**
* Demonstrates how to make the most basic GET directions request using the walking profile.
*
* @throws IOException signals that an I/O exception of some sort has occurred
*/
private static void simpleMapboxDirectionsWalkingRequest() throws IOException {
MapboxDirections.Builder builder = MapboxDirections.builder();
List<Point> coordinates = new ArrayList<>();
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
RouteOptions routeOptions = RouteOptions.builder()
.coordinatesList(coordinates)
.profile("walking")
.walkingSpeed(1.0)
.walkwayBias(0.6)
.alleyBias(0.7)
.build();
builder.routeOptions(routeOptions);
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
// 2. That's it! Now execute the command and get the response.
Response<DirectionsResponse> response = builder.build().executeCall();
// 3. Log information from the response
System.out.printf("%nCheck that the GET response is successful %b", response.isSuccessful());
System.out.printf("%nGet the first routes distance from origin to destination: %f",
response.body().routes().get(0).distance());
}
/**
* Demonstrates how to make the most basic POST directions request.
*
* @throws IOException signals that an I/O exception of some sort has occurred
*/
private static void simpleMapboxDirectionsPostRequest() throws IOException {
MapboxDirections.Builder builder = MapboxDirections.builder();
List<Point> coordinates = new ArrayList<>();
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
RouteOptions routeOptions = RouteOptions.builder()
.coordinatesList(coordinates)
.build();
builder.routeOptions(routeOptions);
builder.usePostMethod(true);
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
Response<DirectionsResponse> response = builder.build().executeCall();
System.out.printf("%nCheck that the POST response is successful %b", response.isSuccessful());
System.out.printf("%nGet the first routes distance from origin to destination: %f",
response.body().routes().get(0).distance());
}
/**
* Demonstrates how to make an asynchronous directions request.
*/
private static void asyncMapboxDirectionsRequest() {
MapboxDirections.Builder builder = MapboxDirections.builder();
// 1. Pass in all the required information to get a route.
List<Point> coordinates = new ArrayList<>();
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
RouteOptions routeOptions = RouteOptions.builder()
.coordinatesList(coordinates)
.profile(DirectionsCriteria.PROFILE_CYCLING)
.steps(true)
.build();
builder.routeOptions(routeOptions);
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
// 2. Now request the route using a async call
builder.build().enqueueCall(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
// 3. Log information from the response
if (response.isSuccessful()) {
System.out.printf("%nGet the street name of the first step along the route: %s",
response.body().routes().get(0).legs().get(0).steps().get(0).name());
}
}
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
System.err.println(throwable);
}
});
}
}