Skip to content

Commit 90e5be1

Browse files
authored
Add optional language parameter to geocoder (#315)
* add new test for geocoding language * add new param to services and clients * expose language param to widget
1 parent db90c81 commit 90e5be1

7 files changed

Lines changed: 131 additions & 10 deletions

File tree

mapbox/libandroid-ui/src/main/java/com/mapbox/services/android/ui/geocoder/GeocoderAdapter.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class GeocoderAdapter extends BaseAdapter implements Filterable {
3939
private double[] bbox;
4040
private Position position;
4141
private int limit;
42+
private String language;
4243
private Call call;
4344

4445
private GeocoderFilter geocoderFilter;
@@ -250,6 +251,35 @@ public void setLimit(int limit) {
250251
this.limit = limit;
251252
}
252253

254+
/**
255+
* @return The locale in which results should be returned.
256+
* @since 2.0.0
257+
*/
258+
public String getLanguage() {
259+
return language;
260+
}
261+
262+
/**
263+
* The locale in which results should be returned.
264+
* <p>
265+
* This property affects the language of returned results; generally speaking,
266+
* it does not determine which results are found. If the Geocoding API does not
267+
* recognize the language code, it may fall back to another language or the default
268+
* language. Components other than the language code, such as the country and
269+
* script codes, are ignored.
270+
* <p>
271+
* By default, this property is set to `null`, causing results to be in the default
272+
* language.
273+
* <p>
274+
* This option is experimental.
275+
*
276+
* @param language The locale in which results should be returned.
277+
* @since 2.0.0
278+
*/
279+
public void setLanguage(String language) {
280+
this.language = language;
281+
}
282+
253283
/**
254284
* Can be used to cancel any calls currently in progress. It's a good idea to include in onDestroy() to prevent
255285
* memory leaks
@@ -405,6 +435,9 @@ protected FilterResults performFiltering(CharSequence constraint) {
405435
if (getLimit() != 0) {
406436
builder.setLimit(limit);
407437
}
438+
if (getLanguage() != null) {
439+
builder.setLanguage(language);
440+
}
408441

409442
call = builder.build().getCall();
410443

mapbox/libandroid-ui/src/main/java/com/mapbox/services/android/ui/geocoder/GeocoderAutoCompleteView.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ public void setLimit(int limit) {
207207
adapter.setLimit(limit);
208208
}
209209

210+
/**
211+
* The locale in which results should be returned.
212+
* <p>
213+
* This property affects the language of returned results; generally speaking,
214+
* it does not determine which results are found. If the Geocoding API does not
215+
* recognize the language code, it may fall back to another language or the default
216+
* language. Components other than the language code, such as the country and
217+
* script codes, are ignored.
218+
* <p>
219+
* By default, this property is set to `null`, causing results to be in the default
220+
* language.
221+
* <p>
222+
* This option is experimental.
223+
*
224+
* @param language The locale in which results should be returned.
225+
* @since 2.0.0
226+
*/
227+
public void setLanguage(String language) {
228+
adapter.setLanguage(language);
229+
}
230+
210231
/**
211232
* Sets the listener that will be notified when the user clicks an item in the drop down list.
212233
*

mapbox/libjava-services-rx/src/main/java/com/mapbox/services/api/rx/geocoding/v5/GeocodingServiceRx.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public interface GeocodingServiceRx {
3131
* @param autocomplete True if you want auto complete.
3232
* @param bbox Optionally pass in a bounding box to limit results in.
3333
* @param limit Optionally pass in a limit the amount of returning results.
34+
* @param language The locale in which results should be returned.
3435
* @return A retrofit Observable object
3536
* @since 1.0.0
3637
*/
@@ -45,7 +46,8 @@ Observable<GeocodingResponse> getObservable(
4546
@Query("types") String types,
4647
@Query("autocomplete") Boolean autocomplete,
4748
@Query("bbox") String bbox,
48-
@Query("limit") String limit);
49+
@Query("limit") String limit,
50+
@Query("language") String language);
4951

5052
/**
5153
* Observable-based interface
@@ -61,6 +63,7 @@ Observable<GeocodingResponse> getObservable(
6163
* @param autocomplete True if you want auto complete.
6264
* @param bbox Optionally pass in a bounding box to limit results in.
6365
* @param limit Optionally pass in a limit the amount of returning results.
66+
* @param language The locale in which results should be returned.
6467
* @return A retrofit Observable object
6568
* @since 1.0.0
6669
*/
@@ -75,5 +78,6 @@ Observable<List<GeocodingResponse>> getBatchObservable(
7578
@Query("types") String types,
7679
@Query("autocomplete") Boolean autocomplete,
7780
@Query("bbox") String bbox,
78-
@Query("limit") String limit);
81+
@Query("limit") String limit,
82+
@Query("language") String language);
7983
}

mapbox/libjava-services-rx/src/main/java/com/mapbox/services/api/rx/geocoding/v5/MapboxGeocodingRx.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public Observable<GeocodingResponse> getObservable() {
6767
builder.getGeocodingTypes(),
6868
builder.getAutocomplete(),
6969
builder.getBbox(),
70-
builder.getLimit());
70+
builder.getLimit(),
71+
builder.getLanguage());
7172

7273
// Done
7374
return observable;
@@ -93,7 +94,8 @@ public Observable<List<GeocodingResponse>> getBatchObservable() {
9394
builder.getGeocodingTypes(),
9495
builder.getAutocomplete(),
9596
builder.getBbox(),
96-
builder.getLimit());
97+
builder.getLimit(),
98+
builder.getLanguage());
9799

98100
// Done
99101
return batchObservable;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public interface GeocodingService {
3131
* @param autocomplete True if you want auto complete.
3232
* @param bbox Optionally pass in a bounding box to limit results in.
3333
* @param limit Optionally pass in a limit the amount of returning results.
34+
* @param language The locale in which results should be returned.
3435
* @return A retrofit Call object
3536
* @since 1.0.0
3637
*/
@@ -46,7 +47,8 @@ Call<GeocodingResponse> getCall(
4647
@Query("types") String types,
4748
@Query("autocomplete") Boolean autocomplete,
4849
@Query("bbox") String bbox,
49-
@Query("limit") String limit);
50+
@Query("limit") String limit,
51+
@Query("language") String language);
5052

5153
/**
5254
* Call-based interface
@@ -62,6 +64,7 @@ Call<GeocodingResponse> getCall(
6264
* @param autocomplete True if you want auto complete.
6365
* @param bbox Optionally pass in a bounding box to limit results in.
6466
* @param limit Optionally pass in a limit the amount of returning results.
67+
* @param language The locale in which results should be returned.
6568
* @return A retrofit Call object
6669
* @since 1.0.0
6770
*/
@@ -77,5 +80,6 @@ Call<List<GeocodingResponse>> getBatchCall(
7780
@Query("types") String types,
7881
@Query("autocomplete") Boolean autocomplete,
7982
@Query("bbox") String bbox,
80-
@Query("limit") String limit);
83+
@Query("limit") String limit,
84+
@Query("language") String language);
8185
}

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import com.mapbox.services.api.MapboxBuilder;
66
import com.mapbox.services.api.MapboxService;
77
import com.mapbox.services.api.ServicesException;
8+
import com.mapbox.services.api.geocoding.v5.gson.CarmenGeometryDeserializer;
9+
import com.mapbox.services.api.geocoding.v5.models.GeocodingResponse;
810
import com.mapbox.services.commons.geojson.Geometry;
911
import com.mapbox.services.commons.models.Position;
1012
import com.mapbox.services.commons.utils.TextUtils;
11-
import com.mapbox.services.api.geocoding.v5.gson.CarmenGeometryDeserializer;
12-
import com.mapbox.services.api.geocoding.v5.models.GeocodingResponse;
1313

1414
import java.io.IOException;
1515
import java.util.List;
@@ -103,7 +103,8 @@ public Call<GeocodingResponse> getCall() {
103103
builder.getGeocodingTypes(),
104104
builder.getAutocomplete(),
105105
builder.getBbox(),
106-
builder.getLimit());
106+
builder.getLimit(),
107+
builder.getLanguage());
107108

108109
return call;
109110
}
@@ -134,7 +135,8 @@ public Call<List<GeocodingResponse>> getBatchCall() {
134135
builder.getGeocodingTypes(),
135136
builder.getAutocomplete(),
136137
builder.getBbox(),
137-
builder.getLimit());
138+
builder.getLimit(),
139+
builder.getLanguage());
138140

139141
return batchCall;
140142
}
@@ -242,6 +244,7 @@ public static class Builder<T extends Builder> extends MapboxBuilder {
242244
private Boolean autocomplete = null;
243245
private String bbox = null;
244246
private String limit = null;
247+
private String language = null;
245248

246249
/**
247250
* Constructor
@@ -513,6 +516,37 @@ public String getLimit() {
513516
return limit;
514517
}
515518

519+
/**
520+
* @return The locale in which results should be returned.
521+
* @since 2.0.0
522+
*/
523+
public String getLanguage() {
524+
return language;
525+
}
526+
527+
/**
528+
* The locale in which results should be returned.
529+
*
530+
* This property affects the language of returned results; generally speaking,
531+
* it does not determine which results are found. If the Geocoding API does not
532+
* recognize the language code, it may fall back to another language or the default
533+
* language. Components other than the language code, such as the country and
534+
* script codes, are ignored.
535+
*
536+
* By default, this property is set to `null`, causing results to be in the default
537+
* language.
538+
*
539+
* This option is experimental.
540+
*
541+
* @param language The locale in which results should be returned.
542+
* @return the current MapboxBuilder instance
543+
* @since 2.0.0
544+
*/
545+
public T setLanguage(String language) {
546+
this.language = language;
547+
return (T) this;
548+
}
549+
516550
public T setClientAppName(String appName) {
517551
super.clientAppName = appName;
518552
return (T) this;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.nio.file.Files;
1919
import java.nio.file.Paths;
2020
import java.util.List;
21+
import java.util.Locale;
2122

2223
import okhttp3.HttpUrl;
2324
import okhttp3.mockwebserver.Dispatcher;
@@ -197,4 +198,26 @@ public void testCountryNotSupported() throws ServicesException, IOException {
197198

198199
assertEquals(0, response.body().getFeatures().size());
199200
}
201+
202+
@Test
203+
public void testLanguage() throws IOException, ServicesException {
204+
MapboxGeocoding clientNoLanguage = new MapboxGeocoding.Builder()
205+
.setAccessToken(ACCESS_TOKEN)
206+
.setLocation("1600 pennsylvania ave nw")
207+
.setBaseUrl(mockUrl.toString())
208+
.build();
209+
210+
// By default no language is included
211+
assertTrue(!clientNoLanguage.getCall().request().url().toString().contains("language=en_GB"));
212+
213+
MapboxGeocoding clientLanguage = new MapboxGeocoding.Builder()
214+
.setAccessToken(ACCESS_TOKEN)
215+
.setLocation("1600 pennsylvania ave nw")
216+
.setLanguage(Locale.UK.toString())
217+
.setBaseUrl(mockUrl.toString())
218+
.build();
219+
220+
// setLanguage() will include the language query parameter
221+
assertTrue(clientLanguage.getCall().request().url().toString().contains("language=en_GB"));
222+
}
200223
}

0 commit comments

Comments
 (0)