Android Places API: Autocomplete with getPlaceByID


Android Places API Autocomplete

A new API was released for Android, by the name of Google Places API. This API offers easy to use methods, through which Google Places data can be accessed on Android. The main advantage of this API is, while developing apps, you may not need to parse data from a web service. As all the data is exchanged through GoogleApiClient automatically. Google places API for android gives full access to Google’s database for places. Although this API is not completely free, and is offered under quota based plans, but still its a good trade off when compared to the earlier approach. Android Places API has many new methods for accessing Google places database, but here we will be discussing Autocomplete feature of it, to get a place Id. Further we will be using this unique Place ID to get place details by ID.

Earlier when we had to detect places around our current location in Android, we had to make an API call to Google Maps APIs for Places, with our current latitude and longitude. But now one of the greatest things about new Android Places API is that, to get places around your current place, you don’t need to pass LatLongs. As the new Android places API handles all of this, and much more by itself. If you wish to know in detail about more features of Google Places API for Android, please refer to this Android Places API tutorial, as here we are focused on getting a place Id using autocomplete feature of Android Places API.

Before starting this Android Places API with autocomplete and getPlaceById example, first include Google Play services in your app, by adding this in the dependencies section:

Add this in application tag of your manifest :

Add these permissions in the manifest:

For more details on using play services please refer to this setting up play services page. Next have a look at the steps to generate Places API for Android key

  1. Create a project in Google Developers console or use an existing one.
  2. Navigate to “APIs & Auth” section, select APIs.
  3. Search and select “Places API for Android”.
  4. Enable it.
  5. Then go to the credentials section.
  6. Create a key for Android application.
  7. Enter your SHA1 fingerprint with package name in the desired format. Use this SHA1 fingerprint tutorial to get your fingerprint.
  8. Paste the API key in a meta tag under the application tag of your Android Manifest as shown below.

 

Android Places API – Autocomplete with Suggestions

As you might be aware, that Google’s Places API for Web has a very powerful autocomplete suggestions tool. But also it was not easy to integrate in an Android app. Now since Google has introduced Places API for Android with support for Autocomplete suggestions, things have gone a little easy. Place Autocomplete API has now been completely reinvented and supports many features like location boundaries, filtering place types and much more. Whats even better is that this API can be directly integrated into an Android AutoCompleteTextView, where Places API suggestions could be shown, like below.

Android Places API Autocomplete

Have a look at the layout:

Before making use of Android Places API for Autocomplete suggestions please remember to show the powered by Google attribution as per guidelines. Also please remember to show third party attributions (if any) as shown above.

Now before getting place id and details from an AutoCompleteTextView, we need to customize an ArrayAdapter which would be attached to this AutoCompleteTextView. This adapter would internally make calls to the Google Places API for suggestions by using GeoDataApi.getAutocompletePredictions() method. Have a look at the code:

The above code simply calls the GeoDataApi.getAutocompletePredictions() method to get the Places API autocomplete suggestions, on change of every character. This API call also supports a timeout limit by using the await(60, TimeUnit.SECONDS) method, where the first argument is used to specify the limit. One of the main things to be noted in the ArrayAdapter above is that, an inner class PlaceAutocomplete is being used to store and return the description and IDs of the places to main activity where the Android Places API would be called once again for selected ID.

 

Android Places API – Get Place by ID

Another API which is offered under the Google Places API for Android suite is Place Details API. This API is also called using the GoogleApiClient, but a place ID is required to make the call. Using GeoDataApi.getPlaceById method we can get all the details of requested place. I would like to remind once again here that, calling this API is not completely free, please refer to this usage limits page. Now lets have a look at the main activity class where both these Autocomplete and Place Details by ID APIs are used in a complementing way.

This would result in a screen like this:

Android Places API Get Place Id

Now lets understand, how to get place suggestions from the API, in an AutoCompleteTextView using the GeoDataApi.getAutocompletePredictions() method and further displaying the detailed information using GeoDataApi.getPlaceById method on screen. To get full understanding on how this works have a look at code sample repository below:

Source Code

 

As you can see the code above, first an instance of GoogleApiClient is created. Further it is set in the adapter as soon as the connection is established using GoogleApiClient.ConnectionCallbacks interface. Next the PlaceArrayAdapter is set using LatLngBounds. A class used for defining latitude and longitude bounds. Here in the Android Places API Autocomplete example above, you can see that I have been using the latitudes and longitudes of Mountain View to set the LatLngBounds. Now to get place details we need to call getPlaceById method in the onItemClick method of AdapterView.OnItemClickListener, which is set while creating the activity. This as a whole would successfully show Google Places API autocomplete predictions in the AutocompleteTextView. But if you would like to use a widget for Autocomplete predictions, refer to this tutorial. Hope this helps. Connect with us on Facebook, Google+ and Twitter for more updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

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

31 thoughts on “Android Places API: Autocomplete with getPlaceByID

  • Karam Alem

    Hi,

    thank for your good work. I just have a simple question I want to add an AutoCompleteFilter to the method, for example I want just the result of airports.

    how I can accomplish this, could you give me an example???

  • Harrison

    Great tutorial

    However when I type text really quickly in the AutoCompleteTextView I get the error:

    java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(-1, class android.widget.ListPopupWindow$DropDownListView) with Adapter(class com.mycompany.appointmentmanager.PlaceArrayAdapter)]

    Any suggestions on a solutions?

  • Ajay Kumar

    Hi Sir,
    Thanks for your Best Tutorial, and you save my Job sir. I am reading many tutorial for Google Place API with Autocomplete TextView but error occur after one day Trying your tutorial, my problem is solved……

    So again thank you sir..

  • karan

    Hello, when i have use this code in eclipse with latest google api then my eclipse is hung at 100% and not run , have you any solution for this

  • Amir

    Hi great tutorial, i have a question : How to add the autocompleteText in action bar in this case ? and how to animate maps to location that is selected by the user in the search ?

  • Raj

    hi,

    If my location is chennai, I need those areas in the first order. kindly let me know how to do like this?? Based on my location which i enter. sorry for my English. pls let me know.

  • rya

    hello!
    thank you for your awesome work. i tried to run your code but it says that the getDescription() method on line 92 of the ‘placeArrayAdapar.java’ file has been deprecated. i tried to use getFullText() instead but i could get around it. i am very much of a beginner in android. Any help in this section would be appreciated.

  • John Ward

    Thank you for this detailed example…it’s exactly what I’ve been looking for. I have one question though. In this block of code:

    Iterator iterator = autocompletePredictions.iterator();
    ArrayList resultList = new ArrayList(autocompletePredictions.getCount());
    while (iterator.hasNext()) {
    AutocompletePrediction prediction = iterator.next();
    resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
    prediction.getDescription()));
    }

    Specifically,
    resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getDescription()));

    I get the compilation error: PlaceAutoComplete has private access in com.google.android.gms.location.places.ui.PlaceAutocomplete;

    I can’t instantiate PlaceAutoComplete, and I can’t find any references to my problem.

    Here’s my import: import com.google.android.gms.location.places.ui.PlaceAutocomplete;

    Here’s my gradle dependency: compile ‘com.google.android.gms:play-services-places:9.6.1’

    Do you have any ideas on what might be the cause of my compile issue?
    Thx.

  • Nikhil

    AutocompletePrediction class does not contain any method like getDescription so “prediction.getDescription()” is giving an error . Can you please describe, what this method do?

  • androidV

    Log.i(LOG_TAG, “Selected: ” + item.description);
    How to split item.description to Address line one, two, three, city, state, country, countrycode, postal code.
    Or how to get Address line one, two, three, city, state, country, countrycode, postal code using placeId .

  • Omar Abdelrazik

    Thank you for the great work!
    However, I always end up getting Status{statusCode=ERROR, resolution=null}.
    @Mohit, can you help me with that?

  • Shiva

    I get the following error while open the Activity at Second time
    java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0
    please help me