private static Gson gsonForPolicy(FieldNamingPolicy fieldNamingPolicy) {
return GSONS.computeIfAbsent(
fieldNamingPolicy,
policy ->
new GsonBuilder()
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeAdapter())
.registerTypeAdapter(Distance.class, new DistanceAdapter())
.registerTypeAdapter(Duration.class, new DurationAdapter())
.registerTypeAdapter(Fare.class, new FareAdapter())
.registerTypeAdapter(LatLng.class, new LatLngAdapter())
.registerTypeAdapter(
AddressComponentType.class, new SafeEnumAdapter<>(AddressComponentType.UNKNOWN))
.registerTypeAdapter(AddressType.class, new SafeEnumAdapter<>(AddressType.UNKNOWN))
.registerTypeAdapter(TravelMode.class, new SafeEnumAdapter<>(TravelMode.UNKNOWN))
.registerTypeAdapter(
LocationType.class, new SafeEnumAdapter<>(LocationType.UNKNOWN))
.registerTypeAdapter(RatingType.class, new SafeEnumAdapter<>(RatingType.UNKNOWN))
.registerTypeAdapter(DayOfWeek.class, new DayOfWeekAdapter())
.registerTypeAdapter(PriceLevel.class, new PriceLevelAdapter())
.registerTypeAdapter(Instant.class, new InstantAdapter())
.registerTypeAdapter(LocalTime.class, new LocalTimeAdapter())
.registerTypeAdapter(
GeolocationApi.Response.class, new GeolocationResponseAdapter())
.setFieldNamingPolicy(policy)
.create());
}
private void checkSydneyResult(GeocodingResult[] results) {
assertNotNull(results);
assertNotNull(results[0].geometry);
assertNotNull(results[0].geometry.location);
assertEquals(-33.8674869, results[0].geometry.location.lat, EPSILON);
assertEquals(151.2069902, results[0].geometry.location.lng, EPSILON);
assertEquals(LocationType.APPROXIMATE, results[0].geometry.locationType);
}
/**
* Reverse geocode restricted by type: <a
* href="https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&location_type=ROOFTOP&result_type=street_address">
* Reverse Geocode (40.714224,-73.961452) with location type of ROOFTOP and result type of
* street_address</a>.
*/
@Test
public void testReverseGeocodeRestrictedByType() throws Exception {
try (LocalTestServerContext sc =
new LocalTestServerContext(
"\n"
+ "{\n"
+ " \"results\" : [\n"
+ " {\n"
+ " \"address_components\" : [\n"
+ " {\n"
+ " \"long_name\" : \"277\",\n"
+ " \"short_name\" : \"277\",\n"
+ " \"types\" : [ \"street_number\" ]\n"
+ " },\n"
+ " {\n"
+ " \"long_name\" : \"Bedford Avenue\",\n"
+ " \"short_name\" : \"Bedford Ave\",\n"
+ " \"types\" : [ \"route\" ]\n"
+ " },\n"
+ " {\n"
+ " \"long_name\" : \"Williamsburg\",\n"
+ " \"short_name\" : \"Williamsburg\",\n"
+ " \"types\" : [ \"neighborhood\", \"political\" ]\n"
+ " },\n"
+ " {\n"
+ " \"long_name\" : \"Brooklyn\",\n"
+ " \"short_name\" : \"Brooklyn\",\n"
+ " \"types\" : [ \"political\", \"sublocality\", \"sublocality_level_1\" ]\n"
+ " },\n"
+ " {\n"
+ " \"long_name\" : \"Kings County\",\n"
+ " \"short_name\" : \"Kings County\",\n"
+ " \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n"
+ " },\n"
+ " {\n"
+ " \"long_name\" : \"New York\",\n"
+ " \"short_name\" : \"NY\",\n"
+ " \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n"
+ " },\n"
+ " {\n"
+ " \"long_name\" : \"United States\",\n"
+ " \"short_name\" : \"US\",\n"
+ " \"types\" : [ \"country\", \"political\" ]\n"
+ " },\n"
+ " {\n"
+ " \"long_name\" : \"11211\",\n"
+ " \"short_name\" : \"11211\",\n"
+ " \"types\" : [ \"postal_code\" ]\n"
+ " }\n"
+ " ],\n"
+ " \"formatted_address\" : \"277 Bedford Ave, Brooklyn, NY 11211, USA\",\n"
+ " \"geometry\" : {\n"
+ " \"location\" : {\n"
+ " \"lat\" : 40.7142205,\n"
+ " \"lng\" : -73.9612903\n"
+ " },\n"
+ " \"location_type\" : \"ROOFTOP\",\n"
+ " \"viewport\" : {\n"
+ " \"northeast\" : {\n"
+ " \"lat\" : 40.71556948029149,\n"
+ " \"lng\" : -73.95994131970849\n"
+ " },\n"
+ " \"southwest\" : {\n"
+ " \"lat\" : 40.7128715197085,\n"
+ " \"lng\" : -73.9626392802915\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " \"place_id\" : \"ChIJd8BlQ2BZwokRAFUEcm_qrcA\",\n"
+ " \"types\" : [ \"street_address\" ]\n"
+ " }\n"
+ " ],\n"
+ " \"status\" : \"OK\"\n"
+ "}\n")) {
LatLng latlng = new LatLng(40.714224, -73.961452);
GeocodingResult[] results =
GeocodingApi.newRequest(sc.context)
.latlng(latlng)
.locationType(LocationType.ROOFTOP)
.resultType(AddressType.STREET_ADDRESS)
.await();
assertNotNull(results);
assertNotNull(Arrays.toString(results));
assertEquals("277 Bedford Ave, Brooklyn, NY 11211, USA", results[0].formattedAddress);
assertEquals(LocationType.ROOFTOP, results[0].geometry.locationType);
assertEquals("ChIJd8BlQ2BZwokRAFUEcm_qrcA", results[0].placeId);
sc.assertParamValue(latlng.toUrlValue(), "latlng");
sc.assertParamValue(LocationType.ROOFTOP.toUrlValue(), "location_type");
sc.assertParamValue(AddressType.STREET_ADDRESS.toUrlValue(), "result_type");
}
}
/**
* Sets the location type. Specifying a type will restrict the results to this type. If multiple
* types are specified, the API will return all addresses that match any of the types.
*
* @param locationTypes The location types to restrict to.
* @return Returns this {@code GeocodingApiRequest} for call chaining.
*/
public GeocodingApiRequest locationType(LocationType... locationTypes) {
return param("location_type", join('|', locationTypes));
}