-
Notifications
You must be signed in to change notification settings - Fork 945
/
RoadsApi.java
229 lines (202 loc) · 8.61 KB
/
RoadsApi.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright 2015 Google Inc. All rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.google.maps;
import static com.google.maps.internal.StringJoin.join;
import com.google.gson.FieldNamingPolicy;
import com.google.maps.errors.ApiError;
import com.google.maps.errors.ApiException;
import com.google.maps.internal.ApiConfig;
import com.google.maps.internal.ApiResponse;
import com.google.maps.model.LatLng;
import com.google.maps.model.SnappedPoint;
import com.google.maps.model.SnappedSpeedLimitResponse;
import com.google.maps.model.SpeedLimit;
/**
* The Google Maps Roads API identifies the roads a vehicle was traveling along and provides
* additional metadata about those roads, such as speed limits.
*
* <p>See also: <a href="https://developers.google.com/maps/documentation/roads">Roads API
* documentation</a>.
*/
public class RoadsApi {
static final String API_BASE_URL = "https://roads.googleapis.com";
static final ApiConfig SNAP_TO_ROADS_API_CONFIG =
new ApiConfig("/v1/snapToRoads")
.hostName(API_BASE_URL)
.supportsClientId(false)
.fieldNamingPolicy(FieldNamingPolicy.IDENTITY);
static final ApiConfig SPEEDS_API_CONFIG =
new ApiConfig("/v1/speedLimits")
.hostName(API_BASE_URL)
.supportsClientId(false)
.fieldNamingPolicy(FieldNamingPolicy.IDENTITY);
static final ApiConfig NEAREST_ROADS_API_CONFIG =
new ApiConfig("/v1/nearestRoads")
.hostName(API_BASE_URL)
.supportsClientId(false)
.fieldNamingPolicy(FieldNamingPolicy.IDENTITY);
private RoadsApi() {}
/**
* Takes up to 100 GPS points collected along a route, and returns a similar set of data with the
* points snapped to the most likely roads the vehicle was traveling along.
*
* @param context The {@link GeoApiContext} to make requests through.
* @param path The collected GPS points as a path.
* @return Returns the snapped points as a {@link PendingResult}.
*/
public static PendingResult<SnappedPoint[]> snapToRoads(GeoApiContext context, LatLng... path) {
return context.get(SNAP_TO_ROADS_API_CONFIG, RoadsResponse.class, "path", join('|', path));
}
/**
* Takes up to 100 GPS points collected along a route, and returns a similar set of data with the
* points snapped to the most likely roads the vehicle was traveling along. Additionally, you can
* request that the points be interpolated, resulting in a path that smoothly follows the geometry
* of the road.
*
* @param context The {@link GeoApiContext} to make requests through.
* @param interpolate Whether to interpolate a path to include all points forming the full
* road-geometry. When true, additional interpolated points will also be returned, resulting
* in a path that smoothly follows the geometry of the road, even around corners and through
* tunnels.
* @param path The path to be snapped.
* @return Returns the snapped points as a {@link PendingResult}.
*/
public static PendingResult<SnappedPoint[]> snapToRoads(
GeoApiContext context, boolean interpolate, LatLng... path) {
return context.get(
SNAP_TO_ROADS_API_CONFIG,
RoadsResponse.class,
"path",
join('|', path),
"interpolate",
String.valueOf(interpolate));
}
/**
* Returns the posted speed limit for given road segments. The provided LatLngs will first be
* snapped to the most likely roads the vehicle was traveling along.
*
* <p>Note: The accuracy of speed limit data returned by the Google Maps Roads API cannot be
* guaranteed. Speed limit data provided is not real-time, and may be estimated, inaccurate,
* incomplete, and/or outdated. Inaccuracies in our data may be reported through <a
* href="https://www.localguidesconnect.com/t5/News-Updates/Exclusive-Edit-a-road-segment-in-Google-Maps/ba-p/149865">
* Google Maps Feedback</a>.
*
* @param context The {@link GeoApiContext} to make requests through.
* @param path The collected GPS points as a path.
* @return Returns the speed limits as a {@link PendingResult}.
*/
public static PendingResult<SpeedLimit[]> speedLimits(GeoApiContext context, LatLng... path) {
return context.get(SPEEDS_API_CONFIG, SpeedsResponse.class, "path", join('|', path));
}
/**
* Returns the posted speed limit for given road segments.
*
* <p>Note: The accuracy of speed limit data returned by the Google Maps Roads API cannot be
* guaranteed. Speed limit data provided is not real-time, and may be estimated, inaccurate,
* incomplete, and/or outdated. Inaccuracies in our data may be reported through <a
* href="https://www.localguidesconnect.com/t5/News-Updates/Exclusive-Edit-a-road-segment-in-Google-Maps/ba-p/149865">
* Google Maps Feedback</a>.
*
* @param context The {@link GeoApiContext} to make requests through.
* @param placeIds The Place ID of the road segment. Place IDs are returned by the {@link
* #snapToRoads(GeoApiContext, com.google.maps.model.LatLng...)} method. You can pass up to
* 100 placeIds with each request.
* @return Returns the speed limits as a {@link PendingResult}.
*/
public static PendingResult<SpeedLimit[]> speedLimits(GeoApiContext context, String... placeIds) {
String[] placeParams = new String[2 * placeIds.length];
int i = 0;
for (String placeId : placeIds) {
placeParams[i++] = "placeId";
placeParams[i++] = placeId;
}
return context.get(SPEEDS_API_CONFIG, SpeedsResponse.class, placeParams);
}
/**
* Returns the result of snapping the provided points to roads and retrieving the speed limits.
*
* @param context The {@link GeoApiContext} to make requests through.
* @param path The collected GPS points as a path.
* @return Returns the snapped points and speed limits as a {@link PendingResult}.
*/
public static PendingResult<SnappedSpeedLimitResponse> snappedSpeedLimits(
GeoApiContext context, LatLng... path) {
return context.get(SPEEDS_API_CONFIG, CombinedResponse.class, "path", join('|', path));
}
/**
* Takes up to 100 GPS points, and returns the closest road segment for each point. The points
* passed do not need to be part of a continuous path.
*
* @param context The {@link GeoApiContext} to make requests through.
* @param points The sequence of points to be aligned to nearest roads
* @return Returns the snapped points as a {@link PendingResult}.
*/
public static PendingResult<SnappedPoint[]> nearestRoads(
GeoApiContext context, LatLng... points) {
return context.get(NEAREST_ROADS_API_CONFIG, RoadsResponse.class, "points", join('|', points));
}
public static class RoadsResponse implements ApiResponse<SnappedPoint[]> {
private SnappedPoint[] snappedPoints;
private ApiError error;
@Override
public boolean successful() {
return error == null;
}
@Override
public SnappedPoint[] getResult() {
return snappedPoints;
}
@Override
public ApiException getError() {
return ApiException.from(error.status, error.message);
}
}
public static class SpeedsResponse implements ApiResponse<SpeedLimit[]> {
private SpeedLimit[] speedLimits;
private ApiError error;
@Override
public boolean successful() {
return error == null;
}
@Override
public SpeedLimit[] getResult() {
return speedLimits;
}
@Override
public ApiException getError() {
return ApiException.from(error.status, error.message);
}
}
public static class CombinedResponse implements ApiResponse<SnappedSpeedLimitResponse> {
private SnappedPoint[] snappedPoints;
private SpeedLimit[] speedLimits;
private ApiError error;
@Override
public boolean successful() {
return error == null;
}
@Override
public SnappedSpeedLimitResponse getResult() {
SnappedSpeedLimitResponse response = new SnappedSpeedLimitResponse();
response.snappedPoints = snappedPoints;
response.speedLimits = speedLimits;
return response;
}
@Override
public ApiException getError() {
return ApiException.from(error.status, error.message);
}
}
}