Skip to content

Commit 3ab841b

Browse files
authored
added reverseMode parameter to Geocoding Service (#843)
1 parent 4b67c3c commit 3ab841b

4 files changed

Lines changed: 93 additions & 4 deletions

File tree

services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/GeocodingCriteria.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ public final class GeocodingCriteria {
9696
*/
9797
public static final String TYPE_POI_LANDMARK = "poi.landmark";
9898

99+
/**
100+
* Filter results by distance.
101+
*
102+
* @since 3.3.0
103+
*/
104+
public static final String REVERSE_MODE_DISTANCE = "distance";
105+
106+
/**
107+
* Filter results by score.
108+
*
109+
* @since 3.3.0
110+
*/
111+
public static final String REVERSE_MODE_SCORE = "score";
112+
99113
private GeocodingCriteria() {
100114
// Empty private constructor
101115
}
@@ -133,4 +147,18 @@ private GeocodingCriteria() {
133147
})
134148
public @interface GeocodingTypeCriteria {
135149
}
150+
151+
152+
/**
153+
* Retention policy for reverseMode filter result types.
154+
*
155+
* @since 3.0.0
156+
*/
157+
@Retention(RetentionPolicy.SOURCE)
158+
@StringDef( {
159+
REVERSE_MODE_DISTANCE,
160+
REVERSE_MODE_SCORE
161+
})
162+
public @interface GeocodingReverseModeCriteria {
163+
}
136164
}

services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/GeocodingService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public interface GeocodingService {
3333
* @param bbox Optionally pass in a bounding box to limit results in.
3434
* @param limit Optionally pass in a limit the amount of returning results.
3535
* @param language The locale in which results should be returned.
36+
* @param reverseMode Set the factors that are used to sort nearby results.
3637
* @return A retrofit Call object
3738
* @since 1.0.0
3839
*/
@@ -48,7 +49,8 @@ Call<GeocodingResponse> getCall(
4849
@Query("autocomplete") Boolean autocomplete,
4950
@Query("bbox") String bbox,
5051
@Query("limit") String limit,
51-
@Query("language") String language);
52+
@Query("language") String language,
53+
@Query("reverseMode") String reverseMode);
5254

5355
/**
5456
* Constructs the html call using the information passed in through the
@@ -66,6 +68,7 @@ Call<GeocodingResponse> getCall(
6668
* @param bbox Optionally pass in a bounding box to limit results in.
6769
* @param limit Optionally pass in a limit the amount of returning results.
6870
* @param language The locale in which results should be returned.
71+
* @param reverseMode Set the factors that are used to sort nearby results.
6972
* @return A retrofit Call object
7073
* @since 1.0.0
7174
*/
@@ -81,5 +84,6 @@ Call<List<GeocodingResponse>> getBatchCall(
8184
@Query("autocomplete") Boolean autocomplete,
8285
@Query("bbox") String bbox,
8386
@Query("limit") String limit,
84-
@Query("language") String language);
87+
@Query("language") String language,
88+
@Query("reverseMode") String reverseMode);
8589
}

services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/MapboxGeocoding.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ protected Call<GeocodingResponse> initializeCall() {
9595
autocomplete(),
9696
bbox(),
9797
limit(),
98-
languages());
98+
languages(),
99+
reverseMode());
99100
}
100101

101102
private Call<List<GeocodingResponse>> getBatchCall() {
@@ -119,7 +120,8 @@ private Call<List<GeocodingResponse>> getBatchCall() {
119120
autocomplete(),
120121
bbox(),
121122
limit(),
122-
languages());
123+
languages(),
124+
reverseMode());
123125

124126
return batchCall;
125127
}
@@ -203,6 +205,9 @@ public Call<List<GeocodingResponse>> cloneBatchCall() {
203205
@Nullable
204206
abstract String languages();
205207

208+
@Nullable
209+
abstract String reverseMode();
210+
206211
@Nullable
207212
abstract String clientAppName();
208213

@@ -499,6 +504,20 @@ public Builder languages(Locale... languages) {
499504
*/
500505
public abstract Builder languages(String languages);
501506

507+
/**
508+
* Set the factors that are used to sort nearby results.
509+
* Options avaliable to pass in include, {@link GeocodingCriteria#REVERSE_MODE_DISTANCE} for
510+
* nearest feature result (default) or {@link GeocodingCriteria#REVERSE_MODE_SCORE}
511+
* the notability of features within approximately 1 kilometer of the queried point
512+
* along with proximity.
513+
*
514+
* @param reverseMode limit geocoding results based on the reverseMode
515+
* @return this builder for chaining options together
516+
* @since 3.3.0
517+
*/
518+
public abstract Builder reverseMode(
519+
@Nullable @GeocodingCriteria.GeocodingReverseModeCriteria String reverseMode);
520+
502521
/**
503522
* Required to call when this is being built. If no access token provided,
504523
* {@link ServicesException} will be thrown.
@@ -552,6 +571,10 @@ public MapboxGeocoding build() {
552571
throw new ServicesException("A query with at least one character or digit is required.");
553572
}
554573

574+
if (geocoding.reverseMode() != null
575+
&& geocoding.limit() != null && !geocoding.limit().equals("1")) {
576+
throw new ServicesException("Limit must be combined with a single type parameter");
577+
}
555578
return geocoding;
556579
}
557580
}

services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/MapboxGeocodingTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,38 @@ public void clientAppName_hasAppInString() throws Exception {
220220
.build();
221221
assertTrue(mapboxGeocoding.cloneCall().request().header("User-Agent").contains("APP"));
222222
}
223+
224+
@Test
225+
public void reverseMode_getsAddedToUrlCorrectly() throws Exception {
226+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
227+
.accessToken(ACCESS_TOKEN)
228+
.baseUrl("https://foobar.com")
229+
.query(Point.fromLngLat(-73.989,40.733))
230+
.reverseMode(GeocodingCriteria.REVERSE_MODE_SCORE)
231+
.build();
232+
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
233+
.contains("reverseMode=score"));
234+
235+
mapboxGeocoding = MapboxGeocoding.builder()
236+
.accessToken(ACCESS_TOKEN)
237+
.baseUrl("https://foobar.com")
238+
.query(Point.fromLngLat(-73.989,40.733))
239+
.reverseMode(GeocodingCriteria.REVERSE_MODE_DISTANCE)
240+
.build();
241+
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
242+
.contains("reverseMode=distance"));
243+
}
244+
245+
@Test
246+
public void reverseMode_onlyLimit1_Allowed() throws Exception {
247+
thrown.expect(ServicesException.class);
248+
thrown.expectMessage(startsWith("Limit must be combined with a single type"));
249+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
250+
.accessToken(ACCESS_TOKEN)
251+
.baseUrl("https://foobar.com")
252+
.query(Point.fromLngLat(-73.989,40.733))
253+
.reverseMode(GeocodingCriteria.REVERSE_MODE_SCORE)
254+
.limit(2)
255+
.build();
256+
}
223257
}

0 commit comments

Comments
 (0)