Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Mapbox welcomes participation and contributions from everyone.

### master

- Added intersection search support to MapboxGeocoding [#1074](https://github.com/mapbox/mapbox-java/pull/1074)

### v4.9.0-alpha.1 - September 4, 2019

- Fixed up bintray publish script, generate sources and javadoc jars #1065
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ public abstract static class Builder {

private List<String> countries = new ArrayList<>();

private List<String> intersectionStreets = new ArrayList<>();

/**
* Perform a reverse geocode on the provided {@link Point}. Only one point can be passed in as
* the query and isn't guaranteed to return a result. If you are an enterprise customer and
Expand Down Expand Up @@ -581,6 +583,22 @@ public abstract Builder reverseMode(

abstract MapboxGeocoding autoBuild();


/**
* Specify the two street names for intersection search.
*
* @param streetOneName First street name of the intersection
* @param streetTwoName Second street name of the intersection
* @return this builder for chaining options together
Comment thread
pengdev marked this conversation as resolved.
* @since 4.10.0
*/
public Builder intersectionStreets(@NonNull String streetOneName,
@NonNull String streetTwoName) {
intersectionStreets.add(streetOneName);
intersectionStreets.add(streetTwoName);
return this;
}

/**
* Build a new {@link MapboxGeocoding} object.
*
Expand All @@ -593,6 +611,11 @@ public MapboxGeocoding build() {
country(TextUtils.join(",", countries.toArray()));
}

if (intersectionStreets.size() == 2) {
query(TextUtils.join(" and ", intersectionStreets.toArray()));
geocodingTypes(GeocodingCriteria.TYPE_ADDRESS);
}

// Generate build so that we can check that values are valid.
MapboxGeocoding geocoding = autoBuild();

Expand All @@ -607,6 +630,22 @@ public MapboxGeocoding build() {
&& geocoding.limit() != null && !geocoding.limit().equals("1")) {
throw new ServicesException("Limit must be combined with a single type parameter");
}

if (intersectionStreets.size() == 2) {
if (!(geocoding.mode().equals(GeocodingCriteria.MODE_PLACES)
|| geocoding.mode().equals(GeocodingCriteria.MODE_PLACES_PERMANENT))) {
throw new ServicesException("Geocoding mode must be GeocodingCriteria.MODE_PLACES "
+ "or GeocodingCriteria.MODE_PLACES_PERMANENT for intersection search.");
}
if (TextUtils.isEmpty(geocoding.geocodingTypes())
|| !geocoding.geocodingTypes().equals(GeocodingCriteria.TYPE_ADDRESS)) {
throw new ServicesException("Geocoding type must be set to Geocoding "
+ "Criteria.TYPE_ADDRESS for intersection search.");
}
if (TextUtils.isEmpty(geocoding.proximity())) {
throw new ServicesException("Geocoding proximity must be set for intersection search.");
}
}
return geocoding;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class GeocodingTestUtils extends TestUtils {
private static final String FORWARD_INVALID = "forward_invalid.json";
private static final String FORWARD_VALID_ZH = "forward_valid_zh.json";
private static final String FORWARD_BATCH_GEOCODING = "geocoding_batch.json";
private static final String FORWARD_INTERSECTION = "forward_intersection.json";

private MockWebServer server;
protected HttpUrl mockUrl;
Expand All @@ -43,6 +44,8 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
response = loadJsonFixture(FORWARD_GEOCODING);
} else if (request.getPath().contains("sandy")) {
response = loadJsonFixture(FORWARD_INVALID);
} else if (request.getPath().contains("%20and%20")) {
response = loadJsonFixture(FORWARD_INTERSECTION);
} else {
response = loadJsonFixture(FORWARD_VALID_ZH);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
package com.mapbox.api.geocoding.v5;

import com.mapbox.api.geocoding.v5.models.GeocodingResponse;
import com.mapbox.core.TestUtils;
import com.mapbox.core.exceptions.ServicesException;
import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.Point;

import org.hamcrest.junit.ExpectedException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import retrofit2.Response;

import static org.hamcrest.Matchers.startsWith;
Expand Down Expand Up @@ -67,7 +58,7 @@ public void executeBatchCall_exceptionThrownWhenModeNotSetCorrectly() throws Exc
}

@Test
public void build_noAccessTokenExceptionThrown() throws Exception {
public void build_noAccessTokenExceptionThrown() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Missing required properties: accessToken");
MapboxGeocoding.builder()
Expand All @@ -77,7 +68,7 @@ public void build_noAccessTokenExceptionThrown() throws Exception {
}

@Test
public void build_invalidAccessTokenExceptionThrown() throws Exception {
public void build_invalidAccessTokenExceptionThrown() {
thrown.expect(ServicesException.class);
thrown.expectMessage(
startsWith("Using Mapbox Services requires setting a valid access token."));
Expand Down Expand Up @@ -113,7 +104,7 @@ public void query_acceptsPointsCorrectly() throws Exception {
}

@Test
public void baseUrl_doesChangeTheRequestUrl() throws Exception {
public void baseUrl_doesChangeTheRequestUrl() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl("https://foobar.com")
Expand All @@ -124,7 +115,7 @@ public void baseUrl_doesChangeTheRequestUrl() throws Exception {
}

@Test
public void country_localeCountryConvertsCorrectly() throws Exception {
public void country_localeCountryConvertsCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -135,7 +126,7 @@ public void country_localeCountryConvertsCorrectly() throws Exception {
}

@Test
public void country_localeCountryHandlesMultipleCountriesCorrectly() throws Exception {
public void country_localeCountryHandlesMultipleCountriesCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -148,7 +139,7 @@ public void country_localeCountryHandlesMultipleCountriesCorrectly() throws Exce
}

@Test
public void proximity_doesGetAddedToUrlCorrectly() throws Exception {
public void proximity_doesGetAddedToUrlCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -160,7 +151,7 @@ public void proximity_doesGetAddedToUrlCorrectly() throws Exception {
}

@Test
public void geocodingTypes_getsAddedToUrlCorrectly() throws Exception {
public void geocodingTypes_getsAddedToUrlCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -172,7 +163,7 @@ public void geocodingTypes_getsAddedToUrlCorrectly() throws Exception {
}

@Test
public void autocomplete_getsAddedToUrlCorrectly() throws Exception {
public void autocomplete_getsAddedToUrlCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -184,7 +175,7 @@ public void autocomplete_getsAddedToUrlCorrectly() throws Exception {
}

@Test
public void bbox_getsFormattedCorrectlyForUrl() throws Exception {
public void bbox_getsFormattedCorrectlyForUrl() {
BoundingBox bbox = BoundingBox.fromLngLats(
-77.083056, 38.908611, -76.997778, 38.959167);
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
Expand All @@ -198,7 +189,7 @@ public void bbox_getsFormattedCorrectlyForUrl() throws Exception {
}

@Test
public void bbox_asPoints_getsFormattedCorrectlyForUrl() throws Exception {
public void bbox_asPoints_getsFormattedCorrectlyForUrl() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -213,7 +204,7 @@ public void bbox_asPoints_getsFormattedCorrectlyForUrl() throws Exception {
}

@Test
public void limit_getsAddedToUrlCorrectly() throws Exception {
public void limit_getsAddedToUrlCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -225,7 +216,7 @@ public void limit_getsAddedToUrlCorrectly() throws Exception {
}

@Test
public void language_getsConvertedToUrlCorrectly() throws Exception {
public void language_getsConvertedToUrlCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -237,7 +228,7 @@ public void language_getsConvertedToUrlCorrectly() throws Exception {
}

@Test
public void clientAppName_hasAppInString() throws Exception {
public void clientAppName_hasAppInString() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
Expand All @@ -249,7 +240,7 @@ public void clientAppName_hasAppInString() throws Exception {
}

@Test
public void reverseMode_getsAddedToUrlCorrectly() throws Exception {
public void reverseMode_getsAddedToUrlCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.baseUrl("https://foobar.com")
Expand All @@ -270,7 +261,7 @@ public void reverseMode_getsAddedToUrlCorrectly() throws Exception {
}

@Test
public void reverseMode_onlyLimit1_Allowed() throws Exception {
public void reverseMode_onlyLimit1_Allowed() {
thrown.expect(ServicesException.class);
thrown.expectMessage(startsWith("Limit must be combined with a single type"));
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
Expand All @@ -296,7 +287,7 @@ public void fuzzyMatchSanity() throws Exception {
}

@Test
public void fuzzyMatch_getsAddedToUrlCorrectly() throws Exception {
public void fuzzyMatch_getsAddedToUrlCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.query("wahsington")
Expand All @@ -307,4 +298,86 @@ public void fuzzyMatch_getsAddedToUrlCorrectly() throws Exception {
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
.contains("fuzzyMatch=true"));
}

@Test
public void intersectionSearchSanity() throws IOException {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.intersectionStreets("Market Street", "Fremont Street")
.proximity("-122.39738575285674,37.792514711136945")
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
.baseUrl(mockUrl.toString())
.build();
assertNotNull(mapboxGeocoding);
Response<GeocodingResponse> response = mapboxGeocoding.executeCall();
assertEquals(200, response.code());
}

@Test
public void intersectionSearchAndAddedCorrectly() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.intersectionStreets("Street one", "Street two")
.proximity("-122.39738575285674,37.792514711136945")
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
.baseUrl(mockUrl.toString())
.build();
assertNotNull(mapboxGeocoding);
assertTrue(mapboxGeocoding.cloneCall().request().url().toString().contains("and"));
}

@Test
public void intersectionSearch_AddIntersectionToQuery() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.intersectionStreets("Market Street", "Fremont Street")
.proximity("-122.39738575285674,37.792514711136945")
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
.baseUrl(mockUrl.toString())
.build();
assertNotNull(mapboxGeocoding);
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
.contains("Market%20Street%20and%20Fremont%20Street"));
}

@Test
public void intersectionSearch_WrongMode() {
thrown.expect(ServicesException.class);
thrown.expectMessage(
startsWith("Geocoding mode must be GeocodingCriteria.MODE_PLACES or GeocodingCriteria.MODE_PLACES_PERMANENT for intersection search."));
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.mode("RandomMode")
.intersectionStreets("Market Street", "Fremont Street")
.proximity("-122.39738575285674,37.792514711136945")
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
.baseUrl(mockUrl.toString())
.build();
}

@Test
public void intersectionSearch_AutoSetGeocodingType() {
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.intersectionStreets("Market Street", "Fremont Street")
.proximity("-122.39738575285674,37.792514711136945")
.baseUrl(mockUrl.toString())
.build();
assertNotNull(mapboxGeocoding);
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
.contains("types=address"));
}

@Test
public void intersectionSearch_EmptyPromixity() {
thrown.expect(ServicesException.class);
thrown.expectMessage(
startsWith("Geocoding proximity must be set for intersection search."));
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.intersectionStreets("Market Street", "Fremont Street")
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
.baseUrl(mockUrl.toString())
.build();
}
}
Loading