Skip to content

Commit

Permalink
Add support for filtering results by layer, country, and source (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahsnow1 authored and ecgreb committed Jan 18, 2017
1 parent 8c79773 commit 15b0b9b
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 3 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/com/mapzen/sample/pelias/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mapzen.pelias.BoundingBox;
import com.mapzen.pelias.Pelias;
import com.mapzen.pelias.PeliasLocationProvider;
import com.mapzen.pelias.SuggestFilter;
import com.mapzen.pelias.gson.Result;
import com.mapzen.pelias.widget.AutoCompleteAdapter;
import com.mapzen.pelias.widget.AutoCompleteListView;
Expand Down Expand Up @@ -82,5 +83,18 @@ private void setupPeliasSearchView() {
});
searchView.setIconifiedByDefault(false);
searchView.setQueryHint(this.getString(R.string.search_hint));
searchView.setSuggestFilter(new SuggestFilter() {
@Override public String getCountryFilter() {
return "FR";
}

@Override public String getLayersFilter() {
return "address,venue";
}

@Override public String getSources() {
return "wof,osm,oa,gn";
}
});
}
}
20 changes: 20 additions & 0 deletions lib/src/main/java/com/mapzen/pelias/Pelias.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public void suggest(String query, Callback<Result> callback) {
suggest(query, locationProvider.getLat(), locationProvider.getLon(), callback);
}

/**
* Returns autocomplete suggestions given a query string. The query will use the
* {@link PeliasLocationProvider} to retrieve a lat/lon to use as a focus point for the request.
* Results will be limited by the layers, country, and sources strings. The callback will be
* notified upon success or failure of query.
*/
public void suggest(String query, String layers, String country, String sources,
Callback<Result> callback) {
service.getSuggest(query, locationProvider.getLat(), locationProvider.getLon(), layers, country,
sources).enqueue(callback);
}

/**
* Requests autocomplete suggestions given a query and lat/lon. The lat/lon is used as a focus
* point for results The callback will be notified upon success or failure of the query.
Expand Down Expand Up @@ -150,6 +162,14 @@ public void reverse(double lat, double lon, Callback<Result> callback) {
service.getReverse(lat, lon).enqueue(callback);
}

/**
* Issues a reverse geocode request given the lat/lon and limited to the sources. The callback
* will be notified upon success or failure of the query.
*/
public void reverse(double lat, double lon, String sources, Callback<Result> callback) {
service.getReverse(lat, lon, sources).enqueue(callback);
}

/**
* Issues a place request for a given global identifier. The callback will be notified upon
* success or failure of the query.
Expand Down
21 changes: 20 additions & 1 deletion lib/src/main/java/com/mapzen/pelias/PeliasService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ public interface PeliasService {
* Asynchronously request autocomplete results given a query and focus point.
*/
@GET("/v1/autocomplete") Call<Result> getSuggest(@Query("text") String query,
@Query("focus.point.lat") double lat, @Query("focus.point.lon") double lon);
@Query("focus.point.lat") double lat,
@Query("focus.point.lon") double lon);

/**
* Asynchronously request autocomplete results given a query andfocus point.
* Limit results by layer and country.
*/
@GET("/v1/autocomplete") Call<Result> getSuggest(@Query("text") String query,
@Query("focus.point.lat") double lat,
@Query("focus.point.lon") double lon,
@Query("layers") String layers,
@Query("boundary.country") String country,
@Query("sources") String source);

/**
* Asynchronously request search results given a query and bounding box.
Expand All @@ -39,6 +51,13 @@ public interface PeliasService {
@GET("/v1/reverse") Call<Result> getReverse(@Query("point.lat") double lat,
@Query("point.lon") double lon);

/**
* Asynchronously issue reverse geocode request.
*/
@GET("/v1/reverse") Call<Result> getReverse(@Query("point.lat") double lat,
@Query("point.lon") double lon,
@Query("sources") String sources);

/**
* Asynchronously request more information about places given their global unique identifiers.
*/
Expand Down
28 changes: 28 additions & 0 deletions lib/src/main/java/com/mapzen/pelias/SuggestFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mapzen.pelias;

/**
* Interface to optionally limit autocomplete results on a
* {@link com.mapzen.pelias.widget.PeliasSearchView}.
*/
public interface SuggestFilter {

/**
* Return a string in the alpha-2 or alpha-3 ISO-3166 country code format.
* @return
*/
String getCountryFilter();

/**
* Return a comma-delimited string. For a list of valid layers, see:
* https://mapzen.com/documentation/search/autocomplete/
* @return
*/
String getLayersFilter();

/**
* Return a comma-delimited string. For a list of valid sources, see:
* https://mapzen.com/documentation/search/reverse/#filter-by-data-source
* @return
*/
String getSources();
}
18 changes: 16 additions & 2 deletions lib/src/main/java/com/mapzen/pelias/widget/PeliasSearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mapzen.pelias.R;
import com.mapzen.pelias.SavedSearch;
import com.mapzen.pelias.SimpleFeature;
import com.mapzen.pelias.SuggestFilter;
import com.mapzen.pelias.gson.Feature;
import com.mapzen.pelias.gson.Result;

Expand Down Expand Up @@ -84,6 +85,7 @@ public void run() {
private OnBackPressListener onBackPressListener;
private boolean cacheSearchResults = true;
private boolean autoKeyboardShow = true;
private SuggestFilter suggestFilter;

private Callback<Result> suggestCallback = new Callback<Result>() {
@Override public void onResponse(Call<Result> call, Response<Result> response) {
Expand Down Expand Up @@ -135,6 +137,14 @@ private void setup() {
setImeOptions(EditorInfo.IME_ACTION_SEARCH);
}

/**
* Set a filter to use when querying for autocomplete results.
* @param suggestFilter
*/
public void setSuggestFilter(SuggestFilter suggestFilter) {
this.suggestFilter = suggestFilter;
}

/**
* Set the list to be used for displaying autocomplete results.
*/
Expand Down Expand Up @@ -261,8 +271,12 @@ private void fetchAutoCompleteSuggestions(String text) {
if (pelias == null) {
return;
}

pelias.suggest(text, suggestCallback);
if (suggestFilter == null) {
pelias.suggest(text, suggestCallback);
} else {
pelias.suggest(text, suggestFilter.getLayersFilter(), suggestFilter.getCountryFilter(),
suggestFilter.getSources(), suggestCallback);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions lib/src/test/java/com/mapzen/pelias/PeliasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,28 @@ public class PeliasTest {
verify(mock).getSuggest(eq("test"), eq(1.0), eq(2.0));
}

@Test public void suggest_getSuggestWithLayersCountrySources() throws Exception {
when(mock.getSuggest(anyString(), anyDouble(), anyDouble(), anyString(), anyString(),
anyString())).thenReturn(new TestCall());
peliasWithMock.setLocationProvider(new TestLocationProvider());
peliasWithMock.suggest("test", "venue", "us", "wof", callback);
verify(mock).getSuggest(eq("test"), eq(1.0), eq(2.0), eq("venue"), eq("us"), eq("wof"));
}

@Test public void reverse_getReverseGeocode() throws Exception {
when(mock.getReverse(anyDouble(), anyDouble())).thenReturn(new TestCall());
peliasWithMock.setLocationProvider(new TestLocationProvider());
peliasWithMock.reverse(30.0, 40.0, callback);
verify(mock).getReverse(eq(30.0), eq(40.0));
}

@Test public void reverse_getReverseGeocodeWithSources() throws Exception {
when(mock.getReverse(anyDouble(), anyDouble(), anyString())).thenReturn(new TestCall());
peliasWithMock.setLocationProvider(new TestLocationProvider());
peliasWithMock.reverse(30.0, 40.0, "wof", callback);
verify(mock).getReverse(eq(30.0), eq(40.0), eq("wof"));
}

@Test public void place_shouldSendSearchRequestToServer() throws Exception {
when(mock.getPlace(anyString())).thenReturn(new TestCall());
peliasWithMock.place("osm:venue:3669115471", callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ private class TestPeliasService implements PeliasService {
return new TestCall();
}

@Override public Call<Result> getSuggest(@Query("text") String query,
@Query("focus.point.lat") double lat, @Query("focus.point.lon") double lon,
@Query("layers") String layers, @Query("boundary.country") String country,
@Query("sources") String source) {
return new TestCall();
}

@Override public Call<Result> getSearch(@Query("text") String query,
@Query("focus.viewport.min_lon") double minLon,
@Query("focus.viewport.min_lat") double minLat,
Expand All @@ -330,6 +337,12 @@ private class TestPeliasService implements PeliasService {
return new TestCall();
}

@Override
public Call<Result> getReverse(@Query("point.lat") double lat, @Query("point.lon") double lon,
@Query("sources") String sources) {
return new TestCall();
}

@Override public Call<Result> getPlace(@Query("ids") String ids) {
return new TestCall();
}
Expand Down Expand Up @@ -376,6 +389,13 @@ private class TestEmptyPeliasService implements PeliasService {
return new TestEmptyCall();
}

@Override public Call<Result> getSuggest(@Query("text") String query,
@Query("focus.point.lat") double lat, @Query("focus.point.lon") double lon,
@Query("layers") String layers, @Query("boundary.country") String country,
@Query("sources") String source) {
return new TestEmptyCall();
}

@Override public Call<Result> getSearch(@Query("text") String query,
@Query("focus.viewport.min_lon") double minLon,
@Query("focus.viewport.min_lat") double minLat,
Expand All @@ -394,6 +414,12 @@ private class TestEmptyPeliasService implements PeliasService {
return new TestEmptyCall();
}

@Override
public Call<Result> getReverse(@Query("point.lat") double lat, @Query("point.lon") double lon,
@Query("sources") String sources) {
return new TestEmptyCall();
}

@Override public Call<Result> getPlace(@Query("ids") String ids) {
return new TestEmptyCall();
}
Expand Down

0 comments on commit 15b0b9b

Please sign in to comment.