Android Google Places API Tutorial to Search Google Places

In this tutorial, we will learn how to implement Autocomplete search box to find Google Places in an Android example using Google Places Api.

 

Introduction

Millions of People use Google to find places like schools and parks, Hotels, etc. Within this tutorial, we will integrate an Android Application with Google using Google API. We Create an autocomplete search box to search Google Places and display its Address or fetch all detail from google. So, first we register android application for Google Places API for Android in Google Developer Console and to display searched place, use TextView widget.

 

google places

 

Create a new Project

Create a new project in an android studio.

 

Register your application for Google Places API for Android in Google Developers Console

  • To register your application for Google Places API in Google Developer Console, got to https://developers.google.com/places/android-api/ and click on the GET A KEY button to get the API key of Google Places.

     

    get key
  •  

  • If you have no any project created in Google Developers Console then click on the Agree and continue button to create a new project in Google Developers Console.

     

    google places APi
  •  

  • Now, select your project where your Application registered and click on the Continue Button

     

    google Developer
  •  

  • We need to create an Android API Key by adding your an Android application Package name and SHA-1 certificate fingerprint.

    • For Windows: : Open your terminal and run keytool -list -v -keystore mystore.keystore to generate your SHA-1 certificate fingerprint.
    • For Ubuntu: Open your terminal and run keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android to generate your SHA-1 certificate fingerprint.



     

    credendtials

     
    google_key

     
    Add key

     
    google credentials

 

Add API Key in your Project

To integrate Google Places API with your an Android application by adding Google API Key in AndroidManifest.xml of your android project. Also add the Internet Permission in AndroidManifest.xml because we will access internet to search Google Places. AndroidManifest.xml file look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.googleplaces">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

Create Layout

We make a process to open search box frame on the click of button and display address of searched place. So, we use TextView and Button widget. Use the following code in your activity_main.xml file

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.googleplaces.MainActivity">

    <Button
        android:id="@+id/findPlace"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Search Google Places"
        android:onClick="findPlace"/>

    <TextView
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="25dp"
        android:layout_below="@+id/findPlace"
        android:id="@+id/searched_address"/>

</RelativeLayout>

 

search button

 

Adding Support Library

We will use Google Play Services. So, we need to add the supporting library of google services. Open your build.gradle file and add the supporting library of google services as dependency of your project.

 

dependencies {
   ...
    compile 'com.google.android.gms:play-services:8.4.0'
   ...
}

 

Create a callback method

We need to create a findPlace() method an it will call when user click on the button. Within this method, we need to start another activity PlaceAutocomplete. This Activity is provide by Google Play Services library and this activity opens a autocomplete search box. By using Search Box, we find Google Places.

 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
     
   /*
    * In this method, Start PlaceAutocomplete activity
    * PlaceAutocomplete activity provides a -
    * search box to search Google places
    */ 
     public void findPlace(View view) {
        try {
            Intent intent =
                    new PlaceAutocomplete
                         .IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                         .build(this);
            startActivityForResult(intent, 1);
        } catch (GooglePlayServicesRepairableException e) {
            // TODO: Handle the error.
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO: Handle the error.
        }
    }
}

 

Handle the Response

Now, We received information of searched place by using onActivityResult() method. Within this method, handle the response data of the acitivity and display the address of place.

 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
     
   /*
    * In this method, Start PlaceAutocomplete activity
    * PlaceAutocomplete activity provides--
    * a search box to search Google places
    */ 
     public void findPlace(View view) {
        try {
            Intent intent =
                    new PlaceAutocomplete
                          .IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                          .build(this);
            startActivityForResult(intent, 1);
        } catch (GooglePlayServicesRepairableException e) {
            // TODO: Handle the error.
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO: Handle the error.
        }
    }

    // A place has been received; use requestCode to track the request.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                // retrive the data by using getPlace() method.
                Place place = PlaceAutocomplete.getPlace(this, data);
                Log.e("Tag", "Place: " + place.getAddress() + place.getPhoneNumber());

                ((TextView) findViewById(R.id.searched_address))
                        .setText(place.getName()+",\n"+
                        place.getAddress() +"\n" + place.getPhoneNumber());

            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(this, data);
                // TODO: Handle the error.
                Log.e("Tag", status.getStatusMessage());

            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }
}

 

google search

google places

google searched address

 
 

 
  



The following two tabs change content below.
Abhay Anand has experience in the field of Software Development. He can program Android, PHP, JavaScript, Html, Css. He develops web and Android applications. You can reach him at anand.abhay1910@gmail.com
  • Go to Our Google Map API Tutorial to implement Google Map in an Android Application.

  • Linda Motaung

    Hi, I have been struggling with this for weeks. I have two textfields: origin and destination address and I want to use google places autocomplete as per the tutorial but the only problem I have is that the results of the first search also populates on the second textfield and I don’t want that. The difference between my implementation and the tutorial is that I am populating the results on editText fields rather than a text view. Here is a skeleton of how a little different my implementation is in the attached screenshots: