Skip to content

Commit

Permalink
Update to Places SDK for Android v2.2.0 (#42)
Browse files Browse the repository at this point in the history
- Add straight line distance to programmatic autocomplete requests
- Add support for restricting Place Autocomplete predictions to multiple countries
See [v2.2.0 release notes](https://developers.google.com/places/android-sdk/releases#february_3_2020_-_v220) for more information.
  • Loading branch information
wangela authored Feb 5, 2020
1 parent 59396e4 commit 86bf84b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compat/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.libraries.places:places-compat:2.1.0'
implementation 'com.google.android.libraries.places:places-compat:2.2.0'
}
2 changes: 1 addition & 1 deletion demo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.libraries.places:places:2.1.0'
implementation 'com.google.android.libraries.places:places:2.2.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.example.placesdemo;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.tasks.Task;
import com.google.android.libraries.places.api.Places;
Expand Down Expand Up @@ -44,6 +45,7 @@
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -129,7 +131,7 @@ private void setupAutocompleteSupportFragment() {
.setPlaceFields(getPlaceFields())
.setText(getQuery())
.setHint(getHint())
.setCountry(getCountry())
.setCountries(getCountries())
.setLocationBias(getLocationBias())
.setLocationRestriction(getLocationRestriction())
.setTypeFilter(getTypeFilter())
Expand Down Expand Up @@ -181,7 +183,7 @@ private void startAutocompleteActivity() {
new Autocomplete.IntentBuilder(getMode(), getPlaceFields())
.setInitialQuery(getQuery())
.setHint(getHint())
.setCountry(getCountry())
.setCountries(getCountries())
.setLocationBias(getLocationBias())
.setLocationRestriction(getLocationRestriction())
.setTypeFilter(getTypeFilter())
Expand All @@ -195,7 +197,8 @@ private void findAutocompletePredictions() {
FindAutocompletePredictionsRequest.Builder requestBuilder =
FindAutocompletePredictionsRequest.builder()
.setQuery(getQuery())
.setCountry(getCountry())
.setCountries(getCountries())
.setOrigin((getOrigin()))
.setLocationBias(getLocationBias())
.setLocationRestriction(getLocationRestriction())
.setTypeFilter(getTypeFilter());
Expand Down Expand Up @@ -242,9 +245,14 @@ private String getHint() {
return getTextViewValue(R.id.autocomplete_hint);
}

@Nullable
private String getCountry() {
return getTextViewValue(R.id.autocomplete_country);
@NonNull
private List<String> getCountries() {
String countryString = getTextViewValue(R.id.autocomplete_country);
if (TextUtils.isEmpty(countryString)) {
return new ArrayList<>();
}

return StringUtil.countriesStringToArrayList(countryString);
}

@Nullable
Expand Down Expand Up @@ -283,6 +291,23 @@ private RectangularBounds getBounds(int resIdSouthWest, int resIdNorthEast) {
return RectangularBounds.newInstance(bounds);
}

@Nullable
private LatLng getOrigin() {
String originStr =
((TextView) findViewById(R.id.autocomplete_location_origin)).getText().toString();
if (TextUtils.isEmpty(originStr)) {
return null;
}

LatLng origin = StringUtil.convertToLatLng(originStr);
if (origin == null) {
showErrorAlert(R.string.error_alert_message_invalid_origin);
return null;
}

return origin;
}

@Nullable
private TypeFilter getTypeFilter() {
Spinner typeFilter = findViewById(R.id.autocomplete_type_filter);
Expand Down
15 changes: 14 additions & 1 deletion demo/app/src/main/java/com/example/placesdemo/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -75,6 +77,15 @@ static LatLng convertToLatLng(@Nullable String value) {
}
}

static List<String> countriesStringToArrayList(String countriesString) {
// Allow these delimiters: , ; | / \
List<String> countries = Arrays.asList(countriesString
.replaceAll("\\s", "|")
.split("[,;|/\\\\]",-1));

return countries;
}

static String stringify(FindAutocompletePredictionsResponse response, boolean raw) {
StringBuilder builder = new StringBuilder();

Expand All @@ -83,7 +94,9 @@ static String stringify(FindAutocompletePredictionsResponse response, boolean ra
.append(" Autocomplete Predictions Results:");

if (raw) {
appendListToStringBuilder(builder, response.getAutocompletePredictions()); } else {
builder.append(RESULT_SEPARATOR);
appendListToStringBuilder(builder, response.getAutocompletePredictions());
} else {
for (AutocompletePrediction autocompletePrediction : response.getAutocompletePredictions()) {
builder
.append(RESULT_SEPARATOR)
Expand Down
14 changes: 14 additions & 0 deletions demo/app/src/main/res/layout/autocomplete_test_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
android:orientation="vertical">

<!--Autocomplete parameters-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/autocomplete_location_origin_label"/>

<EditText
android:id="@+id/autocomplete_location_origin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/autocomplete_location_origin_hint"
android:autofillHints=""
android:imeOptions="actionNext"
android:inputType="text"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
12 changes: 10 additions & 2 deletions demo/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<!-- title for error alerts. -->
<string name="error_alert_title" translatable="false">Uh-Oh!</string>
<!-- message to show for error alerts when location origin is malformed. -->
<string name="error_alert_message_invalid_origin" translatable="false">Unable to parse Location Origin. Expected format: "33, 128"</string>
<!-- message to show for error alerts when location bias or restriction is malformed. -->
<string name="error_alert_message_invalid_bounds" translatable="false">Unable to parse Location Bias or Location Restriction. Expected format: "33, 128"</string>
<!-- message to show for error alerts when a photo size field is malformed. -->
Expand Down Expand Up @@ -38,18 +40,24 @@
<!-- Hint for the autocomplete widget's query field. -->
<string name="autocomplete_hint_hint" translatable="false">Hint</string>

<!-- Hint for the autocomplete prediction's and widget's country field. -->
<string name="autocomplete_country_hint" translatable="false">Country</string>
<!-- Hint for the autocomplete prediction's and widget's countries field. -->
<string name="autocomplete_country_hint" translatable="false">Countries (e.g CH, US, RO)</string>

<!-- Hint Text for the autocomplete prediction's and widget's type filter field. -->
<string name="autocomplete_type_filter" translatable="false">Type Filter</string>

<!-- Label for the autocomplete prediction's and widget's location origin field. -->
<string name="autocomplete_location_origin_label" translatable="false">Location Origin (format: -33.2, 128.2)</string>

<!-- Label for the autocomplete prediction's and widget's location bias related fields. -->
<string name="autocomplete_location_bias_label" translatable="false">Location Bias (format: -33.2, 128.2)</string>

<!-- Label for the autocomplete prediction's and widget's location restriction related fields. -->
<string name="autocomplete_location_restriction_label" translatable="false">Location Restriction (format: -33.2, 128.2)</string>

<!-- Hint for the autocomplete prediction's and widget's location origin field. -->
<string name="autocomplete_location_origin_hint" translatable="false">Origin (lat, lng)</string>

<!-- Hint for the autocomplete prediction's and widget's location southwest field. -->
<string name="autocomplete_location_south_west_hint" translatable="false">South West (lat, lng)</string>

Expand Down

0 comments on commit 86bf84b

Please sign in to comment.