Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main/java/com/google/maps/GeocodingApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.maps.model.GeocodingResponse;
import com.google.maps.model.LatLng;
import com.google.maps.model.LocationType;
import java.util.List;

/** A request for the Geocoding API. */
public class GeocodingApiRequest
Expand Down Expand Up @@ -153,13 +154,29 @@ public GeocodingApiRequest locationType(LocationType... locationTypes) {
}

/**
* Determines whether the address descriptor is returned in the response.
* Determines whether to request address descriptors using {@code
* extra_computations=ADDRESS_DESCRIPTORS}.
*
* @param enableAddressDescriptor Flag to determine whether to return the address descriptor in
* the response.
* <p>Enabling appends {@code ADDRESS_DESCRIPTORS} only if it is not already present. Disabling
* removes all occurrences of {@code ADDRESS_DESCRIPTORS}. Other extra computations retain their
* values and order.
*
* <p>For availability and response details, see <a
* href="https://developers.google.com/maps/documentation/geocoding/guides-v3/address-descriptors/requests-address-descriptors">
* Address descriptors</a>.
*
* @param enableAddressDescriptor Whether to request address descriptors in the response.
* @return Returns this {@code GeocodingApiRequest} for call chaining.
*/
public GeocodingApiRequest enableAddressDescriptor(boolean enableAddressDescriptor) {
return param("enable_address_descriptor", String.valueOf(enableAddressDescriptor));
List<String> computations = params().get("extra_computations");
if (enableAddressDescriptor) {
if (computations == null || !computations.contains("ADDRESS_DESCRIPTORS")) {
paramAddToList("extra_computations", "ADDRESS_DESCRIPTORS");
}
} else if (computations != null) {
computations.removeIf("ADDRESS_DESCRIPTORS"::equals);
}
return this;
}
}
76 changes: 76 additions & 0 deletions src/test/java/com/google/maps/GeocodingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.maps.model.LocationType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import okhttp3.Headers;
Expand Down Expand Up @@ -1211,6 +1212,78 @@ public void testSupportedAddressTypesSynagogue() throws Exception {
}
}

@Test
public void testEnableAddressDescriptorIsIdempotentAcrossToggles() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext(simpleGeocodeResponse)) {
GeocodingApi.newRequest(sc.context)
.address("Sydney")
.enableAddressDescriptor(true)
.enableAddressDescriptor(false)
.enableAddressDescriptor(true)
.enableAddressDescriptor(true)
.await();

sc.assertParamValues(Collections.singletonList("ADDRESS_DESCRIPTORS"), "extra_computations");
}
}

@Test
public void testDisableAddressDescriptorDoesNotSendEmptyParameter() throws Exception {
for (boolean initiallyEnabled : new boolean[] {false, true}) {
try (LocalTestServerContext sc = new LocalTestServerContext(simpleGeocodeResponse)) {
GeocodingApiRequest request = GeocodingApi.newRequest(sc.context).address("Sydney");
if (initiallyEnabled) {
request.enableAddressDescriptor(true);
}
request.enableAddressDescriptor(false).await();

sc.assertParamValues(Collections.emptyList(), "extra_computations");
sc.assertParamValues(Collections.emptyList(), "enable_address_descriptor");
}
}
}

@Test
public void testEnableAddressDescriptorPreservesExistingComputations() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext(simpleGeocodeResponse)) {
GeocodingApi.newRequest(sc.context)
.address("Sydney")
.custom("extra_computations", "test-computation")
.enableAddressDescriptor(true)
.await();

sc.assertParamValues(
Arrays.asList("test-computation", "ADDRESS_DESCRIPTORS"), "extra_computations");
}
}

@Test
public void testDisableAddressDescriptorPreservesOtherComputations() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext(simpleGeocodeResponse)) {
GeocodingApi.newRequest(sc.context)
.address("Sydney")
.custom("extra_computations", "test-computation")
.enableAddressDescriptor(true)
.enableAddressDescriptor(false)
.await();

sc.assertParamValues(Collections.singletonList("test-computation"), "extra_computations");
}
}

@Test
public void testDescriptorHelperRecognizesCustomComputation() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext(simpleGeocodeResponse)) {
GeocodingApi.newRequest(sc.context)
.address("Sydney")
.custom("extra_computations", "ADDRESS_DESCRIPTORS")
.enableAddressDescriptor(true)
.await();

sc.assertParamValues(Collections.singletonList("ADDRESS_DESCRIPTORS"), "extra_computations");
}
}

/** Testing Address Descriptors in reverse geocode. */
@Test
public void testReverseGeocodeWithAddressDescriptors() throws Exception {
Expand All @@ -1236,6 +1309,9 @@ public void testReverseGeocodeWithAddressDescriptors() throws Exception {
assertEquals(AddressComponentType.WARD, results[3].addressComponents[0].types[2]);

sc.assertParamValue(location.toUrlValue(), "latlng");
sc.assertParamValues(Collections.singletonList("ADDRESS_DESCRIPTORS"), "extra_computations");
sc.assertParamValues(Collections.emptyList(), "enable_address_descriptor");
assertEquals("/maps/api/geocode/json", sc.path());

assertNotNull(response.addressDescriptor);
assertEquals("ChIJvUbrwCCoAWARX2QiHCsn5A4", response.addressDescriptor.landmarks[0].placeId);
Expand Down
Loading