Skip to content

Commit 6dab87c

Browse files
tcliff111Tom Cliffordwangela
authored
breaking: Add Address Descriptors to Geocoding response. (#954)
feat: Add Address Descriptors to Geocoding response. BREAKING CHANGE: Refactored Geocoding response to allow fields outside the GeocodingResult to be exposed through the client. Co-authored-by: Tom Clifford <thomasclifford@google.com> Co-authored-by: Angela Yu <5506675+wangela@users.noreply.github.com>
1 parent 10e89b9 commit 6dab87c

10 files changed

Lines changed: 1178 additions & 36 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ This example uses the [Geocoding API] with an API key:
120120
GeoApiContext context = new GeoApiContext.Builder()
121121
.apiKey("AIza...")
122122
.build();
123-
GeocodingResult[] results = GeocodingApi.geocode(context,
123+
GeocodingResponse response = GeocodingApi.geocode(context,
124124
"1600 Amphitheatre Parkway Mountain View, CA 94043").await();
125125
Gson gson = new GsonBuilder().setPrettyPrinting().create();
126-
System.out.println(gson.toJson(results[0].addressComponents));
126+
System.out.println(gson.toJson(response.results[0].addressComponents));
127127

128128
// Invoke .shutdown() after your application is done making requests
129129
context.shutdown();
@@ -205,9 +205,9 @@ try {
205205
req.awaitIgnoreError(); // No checked exception.
206206

207207
// Async
208-
req.setCallback(new PendingResult.Callback<GeocodingResult[]>() {
208+
req.setCallback(new PendingResult.Callback<GeocodingResponse>() {
209209
@Override
210-
public void onResult(GeocodingResult[] result) {
210+
public void onResult(GeocodingResponse result) {
211211
// Handle successful request.
212212
}
213213

src/main/java/com/google/maps/GeocodingApi.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import com.google.maps.errors.ApiException;
1919
import com.google.maps.internal.ApiResponse;
20+
import com.google.maps.model.AddressDescriptor;
21+
import com.google.maps.model.GeocodingResponse;
2022
import com.google.maps.model.GeocodingResult;
2123
import com.google.maps.model.LatLng;
2224

@@ -68,19 +70,24 @@ public static GeocodingApiRequest reverseGeocode(GeoApiContext context, LatLng l
6870
return request;
6971
}
7072

71-
public static class Response implements ApiResponse<GeocodingResult[]> {
73+
/** Requests the street address of a {@code location}. */
74+
public static class Response implements ApiResponse<GeocodingResponse> {
7275
public String status;
7376
public String errorMessage;
74-
public GeocodingResult[] results;
77+
public GeocodingResult results[];
78+
public AddressDescriptor addressDescriptor;
7579

7680
@Override
7781
public boolean successful() {
7882
return "OK".equals(status) || "ZERO_RESULTS".equals(status);
7983
}
8084

8185
@Override
82-
public GeocodingResult[] getResult() {
83-
return results;
86+
public GeocodingResponse getResult() {
87+
GeocodingResponse response = new GeocodingResponse();
88+
response.results = results;
89+
response.addressDescriptor = addressDescriptor;
90+
return response;
8491
}
8592

8693
@Override

src/main/java/com/google/maps/GeocodingApiRequest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
import com.google.maps.internal.ApiConfig;
2121
import com.google.maps.model.AddressType;
2222
import com.google.maps.model.ComponentFilter;
23-
import com.google.maps.model.GeocodingResult;
23+
import com.google.maps.model.GeocodingResponse;
2424
import com.google.maps.model.LatLng;
2525
import com.google.maps.model.LocationType;
2626

2727
/** A request for the Geocoding API. */
2828
public class GeocodingApiRequest
29-
extends PendingResultBase<GeocodingResult[], GeocodingApiRequest, GeocodingApi.Response> {
29+
extends PendingResultBase<GeocodingResponse, GeocodingApiRequest, GeocodingApi.Response> {
3030

3131
private static final ApiConfig API_CONFIG = new ApiConfig("/maps/api/geocode/json");
3232

@@ -151,4 +151,15 @@ public GeocodingApiRequest resultType(AddressType... resultTypes) {
151151
public GeocodingApiRequest locationType(LocationType... locationTypes) {
152152
return param("location_type", join('|', locationTypes));
153153
}
154+
155+
/**
156+
* Determines whether the address descriptor is returned in the response.
157+
*
158+
* @param enableAddressDescriptor Flag to determine whether to return the address descriptor in
159+
* the response.
160+
* @return Returns this {@code GeocodingApiRequest} for call chaining.
161+
*/
162+
public GeocodingApiRequest enableAddressDescriptor(boolean enableAddressDescriptor) {
163+
return param("enable_address_descriptor", String.valueOf(enableAddressDescriptor));
164+
}
154165
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* Copyright 2024 Google Inc. All rights reserved.
3+
*
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6+
* file except in compliance with the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under
11+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12+
* ANY KIND, either express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.google.maps.model;
17+
18+
import java.io.Serializable;
19+
import java.util.Arrays;
20+
21+
/**
22+
* Represents a descriptor of an address.
23+
*
24+
* <p>Please see <a href="https://mapsplatform.google.com/demos/address-descriptors/">Address
25+
* Descriptors</a> for more detail.
26+
*/
27+
public class AddressDescriptor implements Serializable {
28+
29+
private static final long serialVersionUID = 1L;
30+
31+
/** Points of interest that provide a reference point for a location. */
32+
public static class Landmark implements Serializable {
33+
34+
private static final long serialVersionUID = 1L;
35+
36+
/**
37+
* The Place ID of the underlying establishment serving as the landmark. Can be used to resolve
38+
* more information about the landmark through Place Details or Place Id Lookup.
39+
*/
40+
public String placeId;
41+
42+
/** The best name for the landmark. */
43+
public LocalizedText displayName;
44+
45+
/**
46+
* One or more values indicating the type of the returned result. Please see <a
47+
* href="https://developers.google.com/maps/documentation/places/web-service/supported_types">Types
48+
* </a> for more detail.
49+
*/
50+
public String types[];
51+
52+
/** An enum representing the relationship in space between the landmark and the target. */
53+
public enum SpatialRelationship {
54+
/** This is the default relationship when nothing more specific below applies. */
55+
NEAR("near"),
56+
/** The landmark has a spatial geometry and the target is within its bounds. */
57+
WITHIN("within"),
58+
/** The target is directly adjacent to the landmark or landmark's access point. */
59+
BESIDE("beside"),
60+
/** The target is directly opposite the landmark on the other side of the road. */
61+
ACROSS_THE_ROAD("across_the_road"),
62+
/** On the same route as the landmark but not besides or across. */
63+
DOWN_THE_ROAD("down_the_road"),
64+
/** Not on the same route as the landmark but a single 'turn' away. */
65+
AROUND_THE_CORNER("around_the_corner"),
66+
/** Close to the landmark's structure but further away from its access point. */
67+
BEHIND("behind");
68+
69+
private final String spatialRelationship;
70+
71+
SpatialRelationship(final String spatialRelationship) {
72+
this.spatialRelationship = spatialRelationship;
73+
}
74+
75+
@Override
76+
public String toString() {
77+
return spatialRelationship;
78+
}
79+
80+
public String toCanonicalLiteral() {
81+
return toString();
82+
}
83+
}
84+
85+
/** Defines the spatial relationship between the target location and the landmark. */
86+
public SpatialRelationship spatialRelationship;
87+
88+
/**
89+
* The straight-line distance between the target location and one of the landmark's access
90+
* points.
91+
*/
92+
public float straightLineDistanceMeters;
93+
94+
/**
95+
* The travel distance along the road network between the target location's closest point on a
96+
* road, and the landmark's closest access point on a road. This can be unpopulated if the
97+
* landmark is disconnected from the road network the target is closest to OR if the target
98+
* location was not actually considered to be on the road network.
99+
*/
100+
public float travelDistanceMeters;
101+
102+
@Override
103+
public String toString() {
104+
StringBuilder sb = new StringBuilder("[Landmark: ");
105+
if (placeId != null) {
106+
sb.append("placeId=").append(placeId);
107+
}
108+
if (displayName != null) {
109+
sb.append(", displayName=").append(displayName);
110+
}
111+
if (types != null && types.length > 0) {
112+
sb.append(", types=").append(Arrays.toString(types));
113+
}
114+
if (spatialRelationship != null) {
115+
sb.append(", spatialRelationship=").append(spatialRelationship);
116+
}
117+
sb.append(", straightLineDistanceMeters=").append(straightLineDistanceMeters);
118+
sb.append(", travelDistanceMeters=").append(travelDistanceMeters);
119+
sb.append("]");
120+
return sb.toString();
121+
}
122+
}
123+
124+
/**
125+
* A ranked list of nearby landmarks. The most useful (recognizable and nearby) landmarks are
126+
* ranked first.
127+
*/
128+
public Landmark landmarks[];
129+
130+
/** Precise regions that are useful at describing a location. */
131+
public static class Area implements Serializable {
132+
133+
private static final long serialVersionUID = 1L;
134+
135+
/**
136+
* The Place ID of the underlying area feature. Can be used to resolve more information about
137+
* the area through Place Details or Place Id Lookup.
138+
*/
139+
public String placeId;
140+
141+
/** The best name for the area. */
142+
public LocalizedText displayName;
143+
144+
/** An enum representing the relationship in space between the area and the target. */
145+
public enum Containment {
146+
/** Indicates an unknown containment returned by the server. */
147+
CONTAINMENT_UNSPECIFIED("containment_unspecified"),
148+
149+
/** The target location is within the area region, close to the center. */
150+
WITHIN("within"),
151+
152+
/** The target location is within the area region, close to the edge. */
153+
OUTSKIRTS("outskirts"),
154+
155+
/** The target location is outside the area region, but close by. */
156+
NEAR("near");
157+
158+
private final String containment;
159+
160+
Containment(final String containment) {
161+
this.containment = containment;
162+
}
163+
164+
@Override
165+
public String toString() {
166+
return containment;
167+
}
168+
169+
public String toCanonicalLiteral() {
170+
return toString();
171+
}
172+
}
173+
174+
/** Defines the spatial relationship between the target location and the political region. */
175+
public Containment containment;
176+
177+
@Override
178+
public String toString() {
179+
StringBuilder sb = new StringBuilder("[Area: ");
180+
if (placeId != null) {
181+
sb.append("placeId=").append(placeId);
182+
}
183+
if (displayName != null) {
184+
sb.append(", displayName=").append(displayName);
185+
}
186+
if (containment != null) {
187+
sb.append(", containment=").append(containment);
188+
}
189+
sb.append("]");
190+
return sb.toString();
191+
}
192+
}
193+
194+
/**
195+
* A ranked list of containing or adjacent areas. The most useful (recognizable and precise) areas
196+
* are ranked first.
197+
*/
198+
public Area areas[];
199+
200+
@Override
201+
public String toString() {
202+
StringBuilder sb = new StringBuilder("[AddressDescriptor: ");
203+
if (areas != null && areas.length > 0) {
204+
sb.append("areas=").append(Arrays.toString(areas));
205+
}
206+
if (landmarks != null && landmarks.length > 0) {
207+
sb.append(", landmarks=").append(Arrays.toString(landmarks));
208+
}
209+
sb.append("]");
210+
return sb.toString();
211+
}
212+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2024 Google Inc. All rights reserved.
3+
*
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6+
* file except in compliance with the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under
11+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12+
* ANY KIND, either express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.google.maps.model;
17+
18+
import java.io.Serializable;
19+
20+
/**
21+
* The response from a Geocoding request.
22+
*
23+
* <p>Please see <a
24+
* href="https://developers.google.com/maps/documentation/geocoding/requests-geocoding#GeocodingResponses">Geocoding
25+
* Responses</a> for more detail.
26+
*/
27+
public class GeocodingResponse implements Serializable {
28+
29+
private static final long serialVersionUID = 1L;
30+
31+
/** The list of Geocoding Results. */
32+
public GeocodingResult results[];
33+
34+
/** The Address Descriptor for the target. */
35+
public AddressDescriptor addressDescriptor;
36+
37+
@Override
38+
public String toString() {
39+
StringBuilder sb = new StringBuilder("[GeocodingResponse: ");
40+
sb.append(results.length).append(" results");
41+
if (addressDescriptor != null) {
42+
sb.append(", addressDescriptor=").append(addressDescriptor);
43+
}
44+
sb.append("]");
45+
return sb.toString();
46+
}
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 Google Inc. All rights reserved.
3+
*
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6+
* file except in compliance with the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under
11+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12+
* ANY KIND, either express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.google.maps.model;
17+
18+
import java.io.Serializable;
19+
20+
/** Localized variant of a text in a particular language. */
21+
public class LocalizedText implements Serializable {
22+
23+
private static final long serialVersionUID = 1L;
24+
25+
/** Localized string in the language corresponding to language_code below. */
26+
public String text;
27+
28+
/**
29+
* The text's BCP-47 language code, such as "en-US" or "sr-Latn". For more information, see
30+
* http://www.unicode.org/reports/tr35/#Unicode_locale_identifier.
31+
*/
32+
public String languageCode;
33+
34+
@Override
35+
public String toString() {
36+
return String.format("(text=%d, languageCode=%d)", text, languageCode);
37+
}
38+
}

0 commit comments

Comments
 (0)