Android Places Autocomplete Example

Location based application are becoming very common now a days. If we want to implement an application like Uber or Ola which provide cab services user might be needed to input their pick and drop locations and suppose if it is a buy and sell application also user need to input their location. More accurate input will make the business easier.

So it is developers responsibility to provide an interface which user can easily input accurate location.For getting the accurate input location it is always good to have search input which will show list of location suggestion based on users input.

In android , places autocomplete search input can be easily implemented using google API. In this tutorial we will learn how to implement a places autocomplete in android. In order to use Google Places API you need to configure an API key in Google developers console of your account. We will start with configuring API key at console.

Steps for configuring API key

  1. Go to link
  2. Scroll down to section “Get an API key from the Google API Console”.

Places_auto_complete1

3. Click on “Get A Key button”, The following popup will be displayed.

Places_auto_complete2

4.  Here you can create a new project or select an existing project and finally click on “Enable Api” Button.

You must get the following screen

 

5. Note down the the Api key mentioned there.

Next step is to implement Android project

Steps

  1. Create a new project in android studio
  2. Add the location dependency to your apps gradle file.
  3. Add the API key in AndroidManifest.xml
  4. Add autocomplete widget to the layout file.
  5. Implement the code the be executed when the user clicks on suggested item.
  6. How to display places filtered by countries or cities

Create New project in android studio

Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.

Add the location dependency to your apps gradle file

Add the following dependency to your buid.gradile

compile 'com.google.android.gms:play-services-location:9.6.1'
compile 'com.google.android.gms:play-services-places:9.6.1'

Add the API key in AndroidManifest.xml

Add the following tag to your  AndroidManifest.xml under application tag.

<meta-data android:name="com.google.android.geo.API_KEY" android:value="Your api key"/>

Add autocomplete widget to the layout file

Following is a fragment widget which will display the autocomplete fragment in your application .All you need to do is to just copy the following code and add to your activity’s layout file.

<fragment
    android:id="@+id/place_autocomplete_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
    />

 Now if you run the code you can see a search box .When you type on search box it will start listing places.

Implement the code the be executed when the user clicks on suggested item

Once the widget is added to your layout you can see the search box which will show location suggestion based on the keyword user is typing . But for picking the location information of the item which the user is clicked,we need to implement some code on the activity’s java file. It is nothing complicated ,we just need to add the following place selected listener which will give all places information when one place is selected

PlaceAutocompleteFragment places= (PlaceAutocompleteFragment)
        getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
places.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        
        Toast.makeText(getApplicationContext(),place.getName(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(Status status) {
        
        Toast.makeText(getApplicationContext(),status.toString(),Toast.LENGTH_SHORT).show();
        
    }
});
 Run your project ,You will get the following output

 

How to display places filtered by countries.

Now the above code is giving output will places all over the world . Now suppose if you want to display only places in a particular country you need to have a country filter for your autocomplete fragment. Here in the following code i am implementing the filter to list only the places in country US.

AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setCountry("US")
.build();

And another feature of places auto complete fragment is you can filter the places which should be listed in suggestion, Like suppose if you want to show just city name instead of entire address, you can add the filter like this:

AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
        .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
        .build();

places.setFilter(typeFilter);

if you run this code ,it will show only cities. Here is the list of filters you can give inside a AutocompleteFilter

  • TYPE_FILTER_NONE
  • TYPE_FILTER_GEOCODE
  • TYPE_FILTER_ADDRESS
  • TYPE_FILTER_ESTABLISHMENT
  • TYPE_FILTER_REGIONS
  • TYPE_FILTER_CITIES

Refer Android autocomplete textview example if you want to implement normal auto complete textview.

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

71 Comments

  1. jolie April 13, 2018 Reply
  2. Makaveli April 11, 2018 Reply

Add a Comment

Your email address will not be published. Required fields are marked *