Autocomplete places sdk example tutorial

Android AutoComplete Places SDK Example

Google’s AutoComplete service is provided by Places SDK. AutoComplete api is used to auto populate places in response to the user search input. AutoComplete api suggests addresses, location, businesses as the user types and search.

Auto Complete can be added by adding auto complete widget or programatically to create your own user experience.

Places API Key

This tutorial is based on the new Places SDK as Google Play Services version of the Places SDK for Android is deprecated and will be shut off soon.

In order to create location based application we need to first get API key to call Places SDK apis. Register your app with Google Maps Platform and select Places to get your key.

When you create your API key it can be used unrestricted which means any app or website that has access to the key can use it to call Places API. This may result in malicious intent and can incur cost to you. So its always advisable to restrict the key to be used only from your app. Let’s see how to restrict API key usage.

Open API credentials and select the API key. In the next page select Android apps in Application restrictions as shown in the images below.

Google API Credentials

Google Maps API key restriction For Android App AutoComplete Places

SHA-1 Certificate

Create your signing key mystore.keystore. To get SHA-1 certificate fingerprint you need to run this command in the terminal to get the details.

keytool -list -v -keystore app/mystore.keystore

There is another simple way to get the config details directly in Android Studio. Open Gradle and click on signingReport to get fingerprint data.

Generate SHA Signing Report in Android Studio

 

Gradle Dependency

After getting the API key we are ready to move on to the Android application. Add Places SDK gradle dependency in app’s build.gradle

Sync project to apply Places gradle changes.

Android Places SDK

Android AutoComplete Places SDK Example For Location

Initialize Places SDK with the API key as shown in the code below.

Replace GOOGLE_PLACES_API_KEY with your API key. Now we are ready to call Places APIs.

AutoComplete Activity

The code below shows how to open AutoComplete intent and let user search locations based on the input text.

Create activity layout file activity_main.xml

Add Internet permission in AndroidManifest.xml file

Update activity code and call AutoComplete intent after initializing Places SDK.

Place Details

Places SDK can be requested to provide extra information about the place. When Place request is constructed you can provide what type of data to be returned. As in the example above we have added LatLon and Name in the fields to be returned.

In addition to these fields we can request following fields:

  • Place.Field.ADDRESS
  • Place.Field.ID
  • Place.Field.LAT_LNG
  • Place.Field.NAME
  • Place.Field.OPENING_HOURS
  • Place.Field.PHONE_NUMBER
  • Place.Field.PHOTO_METADATAS
  • Place.Field.PLUS_CODE
  • Place.Field.PRICE_LEVEL
  • Place.Field.RATING
  • Place.Field.TYPES
  • Place.Field.USER_RATINGS_TOTAL
  • Place.Field.VIEWPORT
  • Place.Field.WEBSITE_URI

If you have any query or suggestion do comment below. Happy coding!

Leave a Reply

%d bloggers like this: