Google Maps Android API

In the previous tutorial, you have learned about SQLite database in Android along with the basic CRUD operations in Android SQLite. In this tutorial, you will learn about Google Maps Android API. (Read this complete tutorial attentively, for better understanding, as many small problems are identified and solved in this tutorial. Hope that you will not miss a single point)

Introduction to Google Maps Android API

By using Google Maps Android API, you can add maps based on the Google Maps in your Android application. This API has following features

  1. It can access Google Maps Servers.
  2. It can download data from Google Maps.
  3. It can display maps based on Google Maps in your Android Application.
  4. It can respond to map gestures, made during the use of Android application with Google Maps.

Creating a new Android Application with Google Maps

Create a new project if you are using Android Studio.

In Eclipse ADT Bundle, follow the image below to create a new project.

Create a new project in Eclipse ADT Bundle
Create a new project in Eclipse ADT Bundle

 

 

Steps to use Google Maps Android API in Android Application

  1. Install and Configure Google Play Services SDK.
  2. Get an Android Certificate and Google Map Android API key.
  3. Specify app settings in Android application manifest file.

Install and Configure Google Play Services SDK

You need to perform three steps to move further for Installing and Configuring Google Play Services SDK

  1. Install Google Play Services SDK

    For Eclipse ADT Bundle, Launch SDK Manager from  Window -> Android SDK Manager
    Launch SDK Manager in Android Studio from  Tools -> Android -> SDK Manager and open to SDK Tools tab

    SDK Manager SDK Tools
    SDK Manager SDK Tools

    Launch Standalone SDK Manager at the bottom. It will show a new dialog box i.e. Standalone SDK Manager.

    Standalone SDK Manager in Android Studio
    Standalone SDK Manager in Android Studio

    Scroll Down to Extras (Expand Extras) and Install Google Play Services. These Google Play services SDK is saved in Android SDK environment
    at <android‐sdk>/extras/google/google_play_services/

    Note: newer versions of Google Play Services require Android 2.3 or higher. If your app supports Android 2.2, you can continue development with the Google Play services SDK, but must instead install Google Play services for Froyo from the SDK Manager.

  2. Install a Compatible Version of Google APIs Platform

    You must install a compatible version of Google APIs platform.
    If you want to test your application on the emulator, expand the directory for Android 4.2.2 (API level 17) or higher. Select Google APIs and Install it.

    Install Google APIs for Android 6.0 API 23
    Install Google APIs for Android 6.0 API 23

    Now Create a new AVD (Android Virtual Device) from AVD, keep in mind while creating your virtual device that it must have Google APIs as the platform target. You can also use any other Virtual Device like Genymotion (version must be v2.7.0 or higher) or your Android device i.e. phone or tab.

    Creating a new AVD Android Virtual Device with Google APIs (Google Inc.)
    Creating a new AVD Android Virtual Device with Google APIs (Google Inc.)
    • Using Genymotion

      Genymotion is a free tool for personal use, the main purpose of this tool is that it is much faster than the Android Built-in Virtual Device AVD. As AVD shares memory with Android Studio so it is slower. If you want to use Google Map Services on Genymotion, then you must have the Genymotion emulator 2.7.0 or higher.

      To get the latest version of Genymotion click here.  Now download the play services for Genymotion from here. Follow the instructions to enable play services on your Genymotion virtual device i.e. drag and drop the gapps zip files in the running android emulator. The process will flash your virtual device and install Google Play Services. The emulator may give some missing files or errors as the gapps version is very old. Ignore the errors > login with play store > update files. The emulator will start working fine.

  3. Import the Android Play Services Library Project into the Workspace

    Eclipse with ADT bundle: Copy google_play_services_lib library in your workspace. <android-sdk>/extras/google/google_play_services/libproject/googleplay‐services_lib/ To the location where you maintain your Android app projects i.e. Workspace of Android.

    Now go to File -> importAndroid > Existing Android Code into Workspace and browse to the copy of the library you recently imported in the workspace.

    Import Google Play Services Library into Eclipse with ADT Bundle
    Import Google Play Services Library into Eclipse with ADT Bundle

    Android Studio:

    If you don’t want to get screwed with your grade files. Here is an easy approach to import play services files into your project.
    From File choose ‘Project Structure’ or right click on the project and click ‘Open Module Settings’ or press f4.

    Open Android Studio Project Structure from File
    Open Android Studio Project Structure from File

    Go to app from the left menu, Navigate to the Dependencies tab.

    Press ‘+’ sign from top right corner and choose ‘Library Dependency’ from the drop down list. Find the latest version of play services ‘play-services’ from the ‘Choose Library Dependency’ dialog box.

    Add Play Services as Library dependency in Android Studio
    Add Play Services as Library dependency in Android Studio

    Pressing ‘OK’ will add a new play-services dependency in the dependencies list. Press ‘Ok’ to close the Project Structure dialog box. It will automatically inject the dependencies in the build.gradle file.

    Dex Error

    If you want to change dependencies of the project as an alternate of the above import, you can use the procedure highlighted in green. But, keep in mind that if the number of method references increases from 65k it will result in compile time error. To overcome this problem use only specific Google Play Services.
    It will Generate Dex Error: Fix the error by enabling multidex in build.gradle(Module: app) by adding 

    in defaultConfig


    Changing Gradle Files in Android Studio: open your build.grade file under Gradle Scripts and place

    inside the dependencies block (You must update this version as when the Google version changes). Now the dependencies will look like

    For Google Fix and Android Wear APIs instead of the above dependencies, use

     Or you can also place the below code as an alternate to make your app compatible with plugin version.

    If you want to import the files from your lib (library) folder, you can use 

    for more read this article.

You have now completely configured Google Play Services SDK.

Get Android Maps Certificate and Google Maps API Key

For your Google Maps Android application, you must need an Android Certificate. While obtaining a key for your Android application, you must follow several steps.

  1. Retrieve information about your application’s certificate.
  2. Register a project in the Google APIs Console and then add the Maps API as a service for the Google Maps Android project.
  3. You can request one or more keys from Google APIs Console.
  4. Now enter the API key to your Android application, so that you can start developing Google Maps Android Application.

Add Reference to Google Play services in Google Maps Android application

For Eclipse ADT Bundle you must include play services library in your project

Right-click on the  project name -> Click Properties -> Select Android -> Under the Library, Click Add -> Select google‐play-services_lib (which was previously moved to the workspace) -> Click Apply -> Click OK 

Add Google Play Version to Google Maps Android Application’s Manifest. Edit your application’s AndroidManifest.xml file, and add the following declaration within the <application> element. This embeds the version of Google Play services that the app was compiled with.

Get an Android Certificate (SHA1 Fingerprint)

For Eclipse ADT Bundle: Get an Android Certificate. Click Window -> Preferences, select Android > Build, and copy SHA1 fingerprint. 

 

SHA1 fingerprint in Eclipse ADT Bundle
SHA1 fingerprint in Eclipse ADT Bundle

For Android Studio Mac: Use the given command to get the SHA1 fingerprint in Android Studio.

For Android Studio: You can also get it by running the keytool which is placed in C:\Program Files (x86)\Java\jdk[VERSION]\bin\keytool.exe

C:\Program Files (x86)\Java\jdk[VERSION]\bin\keytool.exe

Go to View -> Tool Windows -> Gradle

Open Gradle Projects to get SHA1 fingerprint signing key
Open Gradle Projects to get SHA1 fingerprint signing key

On the right side of the window ‘Gradle Projects’ will be displayed.

Gradle Projects Signing Report to get SHA1 Fingerprint
Gradle Projects Signing Report to get SHA1 Fingerprint

Run the ‘signingReport’  Under -> Tasks -> android -> signingReport. This will display SHA1 fingerprint on the console.

SHA1 fingerprint in Android Studio Console from signingReport
SHA1 fingerprint in Android Studio Console from signingReport

Create an API project in the Google APIs Console

Go to Google APIs Console Panel (Sign into Google Required).

Create a new project (Name the project) go to Maps API and ‘Enable’ Google Maps API.

Click on Google Maps Android API link. Accept All the terms and Conditions after reading.

Google Maps API on developers console
Google Maps API in developers console

Click on ‘Enable’ on the top of the overview screen. Go to ‘Credentials’. ‘Create Credentials’ > ‘API Key’ and Select ‘Android Key’.

In Credentials, Name the Android API Key.

Place Package name (Get your package name by right click on the project ‘Open Module Settings’. In ‘App’ open ‘flavors’ tab, The Application Id is the name of the package of your app) and the SHA1 fingerprint.

Click on ‘Create’ Button to create your credentials. A dialog Box will show the API key.

Create new Credentials to get API key from Google Developers Console
Create new Credentials to get API key from Google Developers Console

Embedding API Key in Google Maps Android Application

To embed the API key of your Android Application, Open AndroidManifest.xml file and place API key in meta description inside meta-data self-closing tags.

Or

In AndroidManifest.xml file, add the following element as a child of the
<application> element, by inserting it just before the closing tag</application>

Substitute the Google API key for API_KEY in the value attribute. This element sets the key com.google.android.maps.v2.API_KEY to the value of your API key, and then makes the API key visible to any MapFragment in your application.

Save the AndroidManifest.xml and re‐build your application.

Configuring Application Settings in the Manifest File

Until now you have successfully configured Google Maps Android API key along with the reference to Google Play Services version. The API key confirms that your Android application is now registered with the Google Maps service via Google Developers API Console.

So now you should give permissions to your Android application for using Android device features and to Google Map servers.

Give Permissions to Android Application

To give permissions that help the application to access Android system features and Google Map servers, it is recommended that notification, the application requires OpenGL ES version 2. So external services can detect this notification and act accordingly. For example, the Google Play Store will not display the application on devices that don’t have OpenGL ES version 2.

For this purpose you must add <usespermission> elements as children of the <manifest> element. These permissions include

  1. Permission to the internet.
  2. Permission to the network state.
  3. Permission to write to external storage.
  4. Permission to access course location and permission to access fine location. (Optional if the application does not access the user’s current location)

Requirement for OpenGL ES version 2

The Google Maps uses OpenGL ES version 2 to render the Maps. If OpenGL ES version 2 is not installed, your map will not appear. It is recommended that you add the following <uses‐feature> element as a child of the <manifest> element in AndroidManifest.xml. This will notify external services of the requirement. Particularly, it has the effect of preventing Google Play Store from displaying your app on devices that don’t support OpenGL ES version 2.

Complete Android Manifest Code

Add Google Map in Android Application

Add simple Google Map in Android application to test the configurations. For this, you only need to make two changes i.e.

In XML you need to add a fragment for Google Maps

And the main Activity Java file

Google Maps Android API in fragment
Google Maps Android API in fragment

Download APK File of this Demo Project

Note: If you use higher SDK version, Android Studio will generate rendering problems sometimes the Maps application might unfortunately stop. Try to use version 16 or 17 for better performance. Rendering issues may appear, but the app will work fine on the emulator or Android device then ignore the rendering problems. If the app still doesn’t work on emulator or device. Keep on testing with the different version or check whether the SDK versions are completely installed or not. Also, Clean and Rebuild the project.

Rendering problem in Google Maps using map fragment (Pick Layout)
Rendering problem in Google Maps using map fragment (Pick Layout)

Complete Source Code of Google Maps Application

build.gradle (Project: AndroidMapDemo)

build.gradle (Module: App)

AndroidManifest.xml

ActivityMain.xml

MainActivity.java

Download APK File of this Demo Project

Download Demo Project of this Application

You can also place marker to Google Map in Android.

 

In the next tutorial, you will learn about Background Services in Android.

Stay tuned for more upcoming tutorials. Stay Blessed!

Leave a Reply

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