AutocompleteLocationAndroid
Cute library to implement PlaceAutocomplete in android based in jotaramirez90 AutocompleteLocation library.
First Steps
In your Project build.gradle you must have the following
buildscript {
...
repositories {
...
jcenter()
}
...
dependencies {
...
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
...
}
In your App build.gradle you must have the following
...
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
...
dependencies {
...
implementation "com.jesualex.android:autocompletelocation:$last_version"
}
Get a Google Maps API Key and enabled the Google Places API for Android (Add your API Key in values):
<resources>
<string name="google_places_key"
translatable="false"
templateMergeStrategy="preserve">YOUR_KEY_HERE</string>
</resources>
Add the AutocompleteLocation into the layout:
<com.jesualex.autocompletelocation.AutocompleteLocation
android:id="@+id/autocomplete_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Set the listeners(All are optional for more comfort):
public class MainActivity extends FragmentActivity
implements AutocompleteLocation.AutocompleteLocationListener {
@Override protected void onCreate(Bundle savedInstanceState) {
...
AutocompleteLocation autoCompleteLocation =
(AutocompleteLocation) findViewById(R.id.autocomplete_location);
autoCompleteLocation.setAutoCompleteTextListener(this);
autoCompleteLocation.setOnSearchListener(this);
}
}
AutoCompleteText Listener:
@Override public void onTextClear() {
mMap.clear();
}
@Override public void onItemSelected(AutocompletePrediction selectedPrediction) {
Log.i(getClass().getSimpleName(), "A autocomplete has selected: ");
logAutocomplete(selectedPrediction);
}
Place Listener(When this listener is set, after selecting a prediction, the associated place is automatically searched):
...
AutocompleteLocation autoCompleteLocation =
(AutocompleteLocation) findViewById(R.id.autocomplete_location);
...
//Set placeListener to auto calculate Place object when a AutocompletePrediction has selected.
autoCompleteLocation.setPlaceListener(this);
//Optional set placeFields to receive only the fields what you need. By default return ID, NAME and LAT_LNG.
autoCompleteLocation.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME););
@Override public void onPlaceLoad(@NonNull Place place) {
addMapMarker(place.getLatLng());
}
Search Listener:
@Override public void onSearch(String address, List<AutocompletePrediction> predictions) {
for (AutocompletePrediction prediction : predictions) {
logAutocomplete(prediction);
}
}
Limit results by Country:
...
AutocompleteLocation autoCompleteLocation =
(AutocompleteLocation) findViewById(R.id.autocomplete_location);
...
autoCompleteLocation.setCountry("Au");
<com.jesualex.autocompletelocation.AutocompleteLocation
android:id="@+id/autocomplete_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:countryCode="au" />
Limit results by Bounds:
...
AutocompleteLocation autoCompleteLocation =
(AutocompleteLocation) findViewById(R.id.autocomplete_location);
...
RectangularBounds bounds = RectangularBounds.newInstance(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596)
);
autoCompleteLocation.setLocationBias(bounds);
Get Place by Id:
PlaceUtils.getPlace(context, "placeId", new OnPlaceLoadListener() {
@Override
public void onPlaceLoad(@NonNull Place place) {
addMapMarker(place.getLatLng());
}
});
Style it!
Attributes for custom AutocompleteLocation
- background_layout
- closeIcon
<com.jesualex.autocompletelocation.AutocompleteLocation
android:id="@+id/autocomplete_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:background_layout="@drawable/bg_rounded_accent"
app:closeIcon="@drawable/ic_close" />