1515
1616package com .google .maps ;
1717
18- import static com .google .maps .internal .StringJoin .join ;
19-
20- import com .google .gson .FieldNamingPolicy ;
2118import com .google .maps .errors .ApiError ;
2219import com .google .maps .errors .ApiException ;
23- import com .google .maps .internal .ApiConfig ;
2420import com .google .maps .internal .ApiResponse ;
2521import com .google .maps .model .LatLng ;
2622import com .google .maps .model .SnappedPoint ;
27- import com .google .maps .model .SnappedSpeedLimitResponse ;
23+ import com .google .maps .model .SnappedSpeedLimitResult ;
2824import com .google .maps .model .SpeedLimit ;
2925
3026/**
3733public class RoadsApi {
3834 static final String API_BASE_URL = "https://roads.googleapis.com" ;
3935
40- static final ApiConfig SNAP_TO_ROADS_API_CONFIG =
41- new ApiConfig ("/v1/snapToRoads" )
42- .hostName (API_BASE_URL )
43- .supportsClientId (false )
44- .fieldNamingPolicy (FieldNamingPolicy .IDENTITY );
45-
46- static final ApiConfig SPEEDS_API_CONFIG =
47- new ApiConfig ("/v1/speedLimits" )
48- .hostName (API_BASE_URL )
49- .supportsClientId (false )
50- .fieldNamingPolicy (FieldNamingPolicy .IDENTITY );
51-
52- static final ApiConfig NEAREST_ROADS_API_CONFIG =
53- new ApiConfig ("/v1/nearestRoads" )
54- .hostName (API_BASE_URL )
55- .supportsClientId (false )
56- .fieldNamingPolicy (FieldNamingPolicy .IDENTITY );
57-
5836 private RoadsApi () {}
5937
6038 /**
@@ -63,10 +41,10 @@ private RoadsApi() {}
6341 *
6442 * @param context The {@link GeoApiContext} to make requests through.
6543 * @param path The collected GPS points as a path.
66- * @return Returns the snapped points as a {@link PendingResult}.
44+ * @return Returns the {@code SnapToRoadsApiRequest} for call chaining
6745 */
68- public static PendingResult < SnappedPoint []> snapToRoads (GeoApiContext context , LatLng ... path ) {
69- return context . get ( SNAP_TO_ROADS_API_CONFIG , RoadsResponse . class , " path" , join ( '|' , path ) );
46+ public static SnapToRoadsApiRequest snapToRoads (GeoApiContext context , LatLng ... path ) {
47+ return snapToRoads ( context , false , path );
7048 }
7149
7250 /**
@@ -81,17 +59,11 @@ public static PendingResult<SnappedPoint[]> snapToRoads(GeoApiContext context, L
8159 * in a path that smoothly follows the geometry of the road, even around corners and through
8260 * tunnels.
8361 * @param path The path to be snapped.
84- * @return Returns the snapped points as a {@link PendingResult}.
62+ * @return Returns the {@code SnapToRoadsApiRequest} for call chaining
8563 */
86- public static PendingResult < SnappedPoint []> snapToRoads (
64+ public static SnapToRoadsApiRequest snapToRoads (
8765 GeoApiContext context , boolean interpolate , LatLng ... path ) {
88- return context .get (
89- SNAP_TO_ROADS_API_CONFIG ,
90- RoadsResponse .class ,
91- "path" ,
92- join ('|' , path ),
93- "interpolate" ,
94- String .valueOf (interpolate ));
66+ return new SnapToRoadsApiRequest (context ).path (path ).interpolate (interpolate );
9567 }
9668
9769 /**
@@ -106,10 +78,10 @@ public static PendingResult<SnappedPoint[]> snapToRoads(
10678 *
10779 * @param context The {@link GeoApiContext} to make requests through.
10880 * @param path The collected GPS points as a path.
109- * @return Returns the speed limits as a {@link PendingResult}.
81+ * @return a {@link SpeedLimitsApiRequest}
11082 */
111- public static PendingResult < SpeedLimit []> speedLimits (GeoApiContext context , LatLng ... path ) {
112- return context . get ( SPEEDS_API_CONFIG , SpeedsResponse . class , " path" , join ( '|' , path ) );
83+ public static SpeedLimitsApiRequest speedLimits (GeoApiContext context , LatLng ... path ) {
84+ return new SpeedLimitsApiRequest ( context ). path ( path );
11385 }
11486
11587 /**
@@ -125,29 +97,10 @@ public static PendingResult<SpeedLimit[]> speedLimits(GeoApiContext context, Lat
12597 * @param placeIds The Place ID of the road segment. Place IDs are returned by the {@link
12698 * #snapToRoads(GeoApiContext, com.google.maps.model.LatLng...)} method. You can pass up to
12799 * 100 placeIds with each request.
128- * @return Returns the speed limits as a {@link PendingResult}.
100+ * @return a {@link SpeedLimitsApiRequest}
129101 */
130- public static PendingResult <SpeedLimit []> speedLimits (GeoApiContext context , String ... placeIds ) {
131- String [] placeParams = new String [2 * placeIds .length ];
132- int i = 0 ;
133- for (String placeId : placeIds ) {
134- placeParams [i ++] = "placeId" ;
135- placeParams [i ++] = placeId ;
136- }
137-
138- return context .get (SPEEDS_API_CONFIG , SpeedsResponse .class , placeParams );
139- }
140-
141- /**
142- * Returns the result of snapping the provided points to roads and retrieving the speed limits.
143- *
144- * @param context The {@link GeoApiContext} to make requests through.
145- * @param path The collected GPS points as a path.
146- * @return Returns the snapped points and speed limits as a {@link PendingResult}.
147- */
148- public static PendingResult <SnappedSpeedLimitResponse > snappedSpeedLimits (
149- GeoApiContext context , LatLng ... path ) {
150- return context .get (SPEEDS_API_CONFIG , CombinedResponse .class , "path" , join ('|' , path ));
102+ public static SpeedLimitsApiRequest speedLimits (GeoApiContext context , String ... placeIds ) {
103+ return new SpeedLimitsApiRequest (context ).placeIds (placeIds );
151104 }
152105
153106 /**
@@ -156,11 +109,10 @@ public static PendingResult<SnappedSpeedLimitResponse> snappedSpeedLimits(
156109 *
157110 * @param context The {@link GeoApiContext} to make requests through.
158111 * @param points The sequence of points to be aligned to nearest roads
159- * @return Returns the snapped points as a {@link PendingResult}.
112+ * @return a {@link NearestRoadsApiRequest}
160113 */
161- public static PendingResult <SnappedPoint []> nearestRoads (
162- GeoApiContext context , LatLng ... points ) {
163- return context .get (NEAREST_ROADS_API_CONFIG , RoadsResponse .class , "points" , join ('|' , points ));
114+ public static NearestRoadsApiRequest nearestRoads (GeoApiContext context , LatLng ... points ) {
115+ return new NearestRoadsApiRequest (context ).points (points );
164116 }
165117
166118 public static class RoadsResponse implements ApiResponse <SnappedPoint []> {
@@ -183,27 +135,7 @@ public ApiException getError() {
183135 }
184136 }
185137
186- public static class SpeedsResponse implements ApiResponse <SpeedLimit []> {
187- private SpeedLimit [] speedLimits ;
188- private ApiError error ;
189-
190- @ Override
191- public boolean successful () {
192- return error == null ;
193- }
194-
195- @ Override
196- public SpeedLimit [] getResult () {
197- return speedLimits ;
198- }
199-
200- @ Override
201- public ApiException getError () {
202- return ApiException .from (error .status , error .message );
203- }
204- }
205-
206- public static class CombinedResponse implements ApiResponse <SnappedSpeedLimitResponse > {
138+ public static class SpeedLimitsResponse implements ApiResponse <SnappedSpeedLimitResult > {
207139 private SnappedPoint [] snappedPoints ;
208140 private SpeedLimit [] speedLimits ;
209141 private ApiError error ;
@@ -214,8 +146,8 @@ public boolean successful() {
214146 }
215147
216148 @ Override
217- public SnappedSpeedLimitResponse getResult () {
218- SnappedSpeedLimitResponse response = new SnappedSpeedLimitResponse ();
149+ public SnappedSpeedLimitResult getResult () {
150+ SnappedSpeedLimitResult response = new SnappedSpeedLimitResult ();
219151 response .snappedPoints = snappedPoints ;
220152 response .speedLimits = speedLimits ;
221153 return response ;
0 commit comments