11package com .mapbox .samples ;
22
3- import com .mapbox .directions .v5 .DirectionsCriteria ;
43import com .mapbox .directions .v5 .MapboxDirections ;
54import com .mapbox .directions .v5 .models .DirectionsResponse ;
65import com .mapbox .geojson .Point ;
76import retrofit2 .Call ;
87import retrofit2 .Callback ;
98import retrofit2 .Response ;
109
10+ import java .io .IOException ;
11+
1112/**
12- * Shows how to make a request using the minimum required params .
13+ * Shows how to make a directions request using some of the parameters offered .
1314 */
14- public class BasicDirections {
15- public static void main (String [] args ) {
16-
17- // 1. Build the directions request using the provided builder.
18- MapboxDirections directions = buildMapboxDirections ();
19-
20- directions .enqueueCall (new Callback <DirectionsResponse >() {
21- @ Override
22- public void onResponse (Call <DirectionsResponse > call , Response <DirectionsResponse > response ) {
23- System .out .println (response .code ());
24- System .out .println (call .request ().url ());
25- System .out .println (response .body ().routes ().get (0 ).legs ().get (0 ).steps ().get (0 ).maneuver ().location ().latitude ());
26- System .out .println (response .body ().routes ().get (0 ).distance ());
27- System .out .println (response .body ().routes ().get (0 ).routeOptions ().profile ());
28- System .out .println (response .body ().routes ().get (0 ).routeOptions ().alternatives ());
29- System .out .println (response .body ().routes ().get (0 ).routeOptions ().user ());
30- System .out .println (response .body ().routes ().get (0 ).legs ().get (0 ).steps ().get (0 ).maneuver ().toString ());
31- System .out .println (response .body ().routes ().get (0 ).legs ().get (0 ).steps ().get (0 )
32- .voiceInstructions ().get (0 ).announcement ());
33- System .out .println (response .body ().routes ().get (0 ).legs ().get (0 ).annotation ().congestion ().size ());
34- System .out .println ("Distance: " + response .body ().routes ().get (0 ).legs ().get (0 ).steps ().get (0 ).bannerInstructions ().get (0 ).distanceAlongGeometry ());
35- }
36-
37- @ Override
38- public void onFailure (Call <DirectionsResponse > call , Throwable throwable ) {
39- System .out .println (call .request ().url ());
40- System .out .println (throwable );
41- }
42- });
15+ public class BasicDirections implements Callback <DirectionsResponse > {
16+
17+ public static void main (String [] args ) throws IOException {
18+
19+ buildMapboxDirectionsRequest ();
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+ }
34+
35+ private static void buildMapboxDirectionsRequest () throws IOException {
36+
37+ MapboxDirections .Builder builder = MapboxDirections .builder ();
38+
39+ // 1. Pass in all the required information to get a simple directions route.
40+ builder .accessToken ("pk.eyJ1IjoiY2FtbWFjZSIsImEiOiI5OGQxZjRmZGQ2YjU3Mzk1YjJmZTQ5ZDY2MTg1NDJiOCJ9.hIFoCKGAGOwQkKyVPvrxvQ" );
41+ builder .origin (Point .fromLngLat (-95.6332 , 29.7890 ));
42+ builder .destination (Point .fromLngLat (-95.3591 , 29.7576 ));
43+
44+ // 2. That's it! Now execute the command and get the response.
45+ Response <DirectionsResponse > response = builder .build ().executeCall ();
46+
47+ // 3. Log information from the response
48+ System .out .println ("Check that the response is successful" + response .isSuccessful ());
49+ System .out .println (("Get the first routes distance from origin to destination: "
50+ + response .body ().routes ().get (0 ).distance ()));
51+
52+
53+
54+ //
55+ //
56+ //
57+ //
58+ //
59+ // return MapboxDirections.builder()
60+ // .origin(Point.fromLngLat(-95.6332, 29.7890))
61+ // .destination(Point.fromLngLat(-95.3591, 29.7576))
62+ // .bannerInstructions(true)
63+ // .voiceInstructions(true)
64+ // .annotations(DirectionsCriteria.ANNOTATION_CONGESTION)
65+ // .overview(DirectionsCriteria.OVERVIEW_FULL)
66+ // .addBearing(null, null)
67+ // .radiuses(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)
68+ // .steps(true)
69+ // .build();
70+ }
71+
72+
73+ @ Override
74+ public void onResponse (Call <DirectionsResponse > call , Response <DirectionsResponse > response ) {
75+
4376 }
4477
45- private static MapboxDirections buildMapboxDirections () {
46- return MapboxDirections .builder ()
47- .accessToken ("pk.eyJ1IjoiY2FtbWFjZSIsImEiOiI5OGQxZjRmZGQ2YjU3Mzk1YjJmZTQ5ZDY2MTg1NDJiOCJ9.hIFoCKGAGOwQkKyVPvrxvQ" )
48- .origin (Point .fromLngLat (-95.6332 , 29.7890 ))
49- .destination (Point .fromLngLat (-95.3591 , 29.7576 ))
50- .bannerInstructions (true )
51- .voiceInstructions (true )
52- .annotations (DirectionsCriteria .ANNOTATION_CONGESTION )
53- .overview (DirectionsCriteria .OVERVIEW_FULL )
54- .addBearing (null , null )
55- .radiuses (Double .POSITIVE_INFINITY , Double .POSITIVE_INFINITY )
56- .steps (true )
57- // .voiceUnits(DirectionsCriteria.METRIC)
58- .build ();
78+ @ Override
79+ public void onFailure (Call <DirectionsResponse > call , Throwable t ) {
80+
5981 }
6082}
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+ // // 1. Build the directions request using the provided builder.
115+ // MapboxDirections directions = buildMapboxDirections();
116+ //
117+ //// 2. Use either
118+ // directions.enqueueCall(new Callback<DirectionsResponse>() {
119+ //@Override
120+ //public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
121+ // System.out.println(response.code());
122+ // System.out.println(call.request().url());
123+ // System.out.println(response.body().routes().get(0).legs().get(0).steps().get(0).maneuver().location().latitude());
124+ // System.out.println(response.body().routes().get(0).distance());
125+ // System.out.println(response.body().routes().get(0).routeOptions().profile());
126+ // System.out.println(response.body().routes().get(0).routeOptions().alternatives());
127+ // System.out.println(response.body().routes().get(0).routeOptions().user());
128+ // System.out.println(response.body().routes().get(0).legs().get(0).steps().get(0).maneuver().toString());
129+ // System.out.println(response.body().routes().get(0).legs().get(0).steps().get(0)
130+ // .voiceInstructions().get(0).announcement());
131+ // System.out.println(response.body().routes().get(0).legs().get(0).annotation().congestion().size());
132+ // System.out.println("Distance: " + response.body().routes().get(0).legs().get(0).steps().get(0).bannerInstructions().get(0).distanceAlongGeometry());
133+ // }
134+ //
135+ //@Override
136+ //public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
137+ // System.out.println(call.request().url());
138+ // System.out.println(throwable);
139+ // }
140+ // });
0 commit comments