Coding, How-To

Using Google Places API for Android

It’s been close to 4 years since I last developed an Android app. I released a private fun app for my own wedding, involving a connection to my VPS, and my own laptop. I could talk about it some other time, since I intend to replicate a better version of the app someday. Would be a great app for events!

Anyway, I’m doing another project now. Realised I’m super rusty in Android and writing mobile apps, so I sorta have to start all over. Many things have also changed since. But well, taking it a step at a time, and Google sure is a helpful tool to help me refresh my mind. My current app involves using Place Autocomplete service in Google Places API for Android. Here are the exact steps on how to get started.


  1. Make Sure You Have Google Play Services Installed

    Go to SDK Manager -> SDK Tools, then check Google Play Services if it is not yet installed. Click on the Apply button at the bottom to install it.

  2. Get Your App’s SHA-1 Fingerprint (Debug Certificate)

    I’m assuming you’re still at the debug stage of your app, and not releasing the app yet. The process for these two are different.
    Open your terminal/cmd and navigate to your debug keystore file. By default, it is in the same directory as your Android Virtual Device (AVD) files:

    • Mac OS or Linux: ~/.android/
    • Windows: C:\Users\your_user_name\.android\

    Get the SHA-1 fingerprint:

    • Mac OS or Linux: keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    • Windows: keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

    You will need the SHA-1 fingerprint in the next step.

  3. Get Your API Key From the Google API Console

    Go to Google API Console, then select an existing project or create a new one.

    After clicking the Continue button, you will see your project dashboard page. Click on the Credentials icon (it’s a key icon) on the left side of the screen.

    Scroll to the Key Restrictions section of the page, and select the Application Restrictions tab. Select Android apps, so that your API key can only be used by Android apps. Then add your package name (found in your AndroidManifest.xml file in your Android project), and your app’s SHA-1 fingerprint (generated in point 2).

    Remember to click Save.

  4. Add Your API Key to Your Android App

    Note your API key generated in the previous step. Now go to the AndroidManifest.xml file in your Android project again. Within the application tag, add the following:

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

    Replace YOUR_API_KEY in android:value with the API key you copied over from step 3.

Now you’re ready to start writing your Android app using the Google Places API for Android! Check out the documentation here for more details. Enjoy!



Leave a Reply

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