Using the New Google Places API for Android


Android Places API for Android

 

One of the most recent developments in the world of Android has been the release of Google Places API for Android. This new API gives us the full access to Google’s database for 100 million places. What’s great about this API is that, to receive data from Google, no XML, or Json parsing needs to be done on the client. All the request and response parsing is done by the Android Places API client itself. It gives us, many new convenience methods through which we can access places API data directly. All we need to do is, use an API key in the project.

The release of Android places API not only simplifies the data access but also spares the developers from keeping track of latitudes and longitudes. Earlier, to access place data in Android, one had to retrieve all the information from a web service by passing various parameters like latitudes and longitudes to it. The API used at that time was Google Maps API. But now thankfully, nothing of this sort needs to be done. As the new Android places API is powerful enough to detect your current location and retrieve all the place data automatically. Google Places API for Android includes six new features:

 

  1. Place Picker UI widget – A great new UI control, which gives the flexibility of selecting a place from nearby places.
  2. Current Place – An API method though which nearby places can be retrieved.
  3. Place Autocomplete – An API method, designed to give suggestions in the AutocompleteTextView.
  4. Place Add – An API method for adding places in the Google Places database.
  5. Place Report – An API method for reviewing places by creating a place report.
  6. Place Details – One of the most basic methods, used to retrieve place details by its ID.

 

Although discussing all of them in a single article would not be possible. Therefore we will be discussing only some of the important features of Android Places API here. But before starting, lets add Google play services and generate an API key first:

Add this in dependencies section:

Add this in application tag of your manifest :

Add these permissions in the manifest:

For more details please refer to this setting up play services page. Next lets 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.

 

Google Places API for Android : Place Picker

PlacePicker is a new UI widget introduced with Places API. This widget can greatly reduce the development efforts in a scenario where requirement is to select a nearby place. Android PlacePicker widget, displays a map and a list of nearby places. By this widget user can pick a place, whose details would be returned to the calling activity. Another great feature about PlacePicker is that, it allows the customization of its headers as well. For example if you have set the colorPrimary and colorPrimaryDark properties in the styles.xml. They would be inherited in the PlacePicker UI as-well, just to maintain consistency in the application flow.

To start, lets design a simple layout to launch PlacePicker and display the results:

Please note:

  • According to Google, when using PlacePicker UI to to pick a place, a powered by Google attribution image needs to be shown on the screen. Also if any third party attributions are present, they are also required to be shown as per these policies.
  • To use place picker UI widget, an API key is also required.

 

Next lets have a look at the main activity:

The above code fires an intent to launch PlacePicker widget, which returns the data for the selected place. A point to note here is that, bounds for place picker can be set through the setLatLngBounds method. As shown this method allows us to launch place picker for a specific area as well. Next, have a look at the Google Places API for Android, PlacePicker screenshots:

 

This slideshow requires JavaScript.

Source code for Android place picker widget example using Google Places API:

Source Code

 

Google Places API for Android : Current Place

PlaceDetectionApi.getCurrentPlace() is another useful API method introduced with this new Android places API. This API returns nearby places of the device. Android current place API, does not require you to explicitly define the location in the request. Place detection API, has the ability to detect your location automatically from your device and return the places nearby. This makes this API very fast and easy to implement. As with earlier Google Maps API’s, we had to detect our latitude, longitude and pass them as a parameter in the request.

The current place detection API is likely to return multiple current places in a PlaceLikelihoodBuffer list, of PlaceLikelihood objects. As its not possible at times to detect the exact location. A PlaceLikelihood object basically contains a probability of correctness, on a scale of 0 to 1.0 and the place data. Where 1 is being the most likely to be the current place. The Current place API for Android also requires a Google API hit. Therefore before making a request you may need to configure a Google API project for it. To generate an API key please refer to the steps above in the first section.

 

We already have the permissions defined in the manifest, hence lets move on to the main class:

Also in the above piece of code you may observe, that its not that direct to request the current place. Now first you need to request the Manifest.permission.ACCESS_FINE_LOCATION permission from the user. If the permission is granted then we can go ahead and request the current place. Layout for Current place detection API:

It has a comparatively simple layout file as, this example prints all locations in the log. Also please don’t forget to add attributions according to these policies.

Android Places API Current Place

In the current place detection API above, you may have noticed that no HttpRequest code is present. This is the beauty of this new Android places API. All you need to do is create an object of GoogleApiClient and add Places.PLACE_DETECTION_API with enableAutoManage, and by using this you can make successful API calls. To fully understand the working have a look at the code sample repository:

Source Code

 

Conclusion

To conclude, I would like to say that Google Places API for Android is a new and improved version of previous maps API. As this new API provides direct integration with Android, which makes the development process very simple. Also a great feature which comes along with this API, is the themes integration for PlacePicker. This is some thing completely new in Android. This feature supports the usage of consistent user interfaces, giving a better user experience in the app. Hope this helped. 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 *

32 thoughts on “Using the New Google Places API for Android

  • Prisma

    I got some error in XML Layout (android:src=”@mipmap/truiton_short_no_back”). Did you miss something to add here?
    Thanks 🙂

  • Audhil

    Hi, I tried without API_KEY for Place Picker widget but it fails. Then tried with obtained API_KEY from Google account now it is working as expected. Any thoughts on this. I saw many more tutorials about Google Places starts with obtaining API_KEY. Kindly share your thoughts here. Thanks.

  • Ari

    Thanks for the article and insight! I’ve been trying to run this code but when I click the “Launch Places API Picker” button to open places, places will not open. Did you experience any issues like this?

  • Darrel

    Hi, That was really helpful.
    But the particular Place picker UI flashes only for a few seconds & returns to the home.I’m unable to figure out the reason.Please could you help me out on this.

    Thanks in advance.

  • XYX

    It’s great tutorial but only one problem that is you should change meta tag in this format ” ” after this screen will not be disappear. Happy Coding.. 🙂

  • Shikhar

    Sir If can Sort out my problem ill be very happy, i have a data of lattitude and longitude i want to show it on map, as a small part in my layout , i want to use this new android place lib.. can you plz help me out how do i do that?

  • Mainak Dev

    Hey i am trying to use Places API for eclipse, i got your entire tutorial but about the build.gradle what do i do with that, as i am using eclipse with adt plugin it dose’nt have build.gradle, can you help me as to what should i do?

  • Pratik

    Hey Great tutorial it works just fine!!!
    but can u plz suggest me is there a way to modify the search result…
    for example im just trying to make a app which should list nearby hospitals
    so is there a way to display only nearby hospitals in “Nearby Places” tab in place Picker
    and also in search bar it should list hospitals names
    plz reply asap thankyou

  • Zafir

    I’m try to do this in a Fragment.
    Where it is asking for @NotNull Activity in the intentBuilder.build(), what do I put? And other places that need context?
    This is for the Place Picker.

  • preet

    hii, i want to say thankss ….and i want to ask thing..what i have to do if i need only particular kind of place like any restaurent any cafe etc…can u plz help…i love your blog.it really helpful..plzz ans me either on my mail or here.

  • duongthehien

    Great tutorial? I have only question.

    How can we show only coffe shop, or bank, but not show all of them in maps.

    Sorry for my bad english

    Thanks you

  • Martin

    Hey Mohit, thanks for the awesome tutorial!

    One problem ive got is the CurrentPlace apk. I copied and pasted 3 times the code from here and the code from gitHub into my Android Studio. When i start this APK, i see the “Get Current Place” Button and when i click on the button, NOTHING happens.. There is not even a error message in Android Studio or something.

    Tried it also on my mobile phone and same there.. when i click on the button, nothing happens, the app doesnt crash or the view doesnt change.

    Any ideas how to solve it?

  • nabil

    Very useful tutorial.

    I got everything as i needed, but i dont want to set the country and state name to autocompletetextview, how can i do that?

  • Rick Klein

    The Place Picker doesn’t display the place the user selected. The map always displays the t longitude and latitude set but not the place the user has selected. How can we fix this?

  • Dhruv

    Just wanted to leave a comment: this is the only tutorial that was simple and actually worked for me. Everything else I’ve found is years old. Thank you so much!