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
- It can access Google Maps Servers.
- It can download data from Google Maps.
- It can display maps based on Google Maps in your Android Application.
- 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.

Steps to use Google Maps Android API in Android Application
- Install and Configure Google Play Services SDK.
- Get an Android Certificate and Google Map Android API key.
- 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
-
Install Google Play Services SDK
For Eclipse ADT Bundle, Launch SDK Manager from
Window -> Android SDK Manager
Launch SDK Manager in Android Studio fromTools -> Android -> SDK Manager
and open to SDK Tools tabSDK 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 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.
-
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 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.) -
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.
-
-
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 -> import. Android > 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 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 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 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 adding1multiDexEnabled truein defaultConfig
123456789defaultConfig {applicationId "com.example.muneeb.androidmapdemo"minSdkVersion 13targetSdkVersion 24versionCode 1versionName "1.0"multiDexEnabled true}
Changing Gradle Files in Android Studio: open your build.grade file under Gradle Scripts and place
1compile 'com.google.android.gms:play-services:9.2.1'<span style="color: #99cc00;"> </span>inside the dependencies block (You must update this version as when the Google version changes). Now the dependencies will look like
1234567dependencies{compile fileTree(include: ['*.jar'], dir: 'libs')testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:24.1.0'compile 'com.google.android.gms:play-services:9.2.1'}For Google Fix and Android Wear APIs instead of the above dependencies, use
12compile 'com.google.android.gms:play-services-fitness:9.2.1'compile 'com.google.android.gms:play-services-wearable:9.2.1'Or you can also place the below code as an alternate to make your app compatible with plugin version.
1234dependencies {compile 'com.android.support:appcompat-v7:+'compile 'com.google.android.gms:play-services:4.3.+'}If you want to import the files from your lib (library) folder, you can use
1234dependencies {compile files('libs/android-support-v4.jar')compile files('libs/google-play-services.jar')}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.
- Retrieve information about your application’s certificate.
- Register a project in the Google APIs Console and then add the Maps API as a service for the Google Maps Android project.
- You can request one or more keys from Google APIs Console.
- 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.
1 2 3 |
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> |
Get an Android Certificate (SHA1 Fingerprint)
For Eclipse ADT Bundle: Get an Android Certificate. Click Window -> Preferences, select Android > Build, and copy SHA1 fingerprint.

For Android Studio Mac: Use the given command to get the SHA1 fingerprint in Android Studio.
1 |
<span class="pln">keytool </span><span class="pun">-</span><span class="pln">list </span><span class="pun">-</span><span class="pln">v </span><span class="pun">-</span><span class="pln">keystore </span><span class="pun">~</span><span class="str">/.android/</span><span class="pln">debug</span><span class="pun">.</span><span class="pln">keystore </span><span class="pun">-</span><span class="kwd">alias</span><span class="pln"> androiddebugkey </span><span class="pun">-</span><span class="pln">storepass android </span><span class="pun">-</span><span class="pln">keypass android</span> |
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

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

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

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.

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.

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.
1 2 3 4 |
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzasdasfdsfsdfSADWdsjajdqZj1ehxu7r7eRBTs" /> |
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>
1 2 3 |
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="API_KEY"/> |
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
- Permission to the internet.
- Permission to the network state.
- Permission to write to external storage.
- Permission to access course location and permission to access fine location. (Optional if the application does not access the user’s current location)
1 2 3 4 5 6 7 8 9 10 11 |
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES "/> <!-- The following two permissions are not required to use Google Maps Android API, but are recommended. To get user's current location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_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.
1 2 3 |
<uses-feature android:glEsVersion="0x00020000" android:required="true"/> |
Complete Android Manifest Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.muneeb.androidmapdemo" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyDbdasD9ZosHKaASuqZj1ehxuyaWGTs"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> </application> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> </manifest> |
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
1 2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.MapFragment"/> |
And the main Activity Java file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.example.muneeb.androidmapdemo; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |

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.

Complete Source Code of Google Maps Application
build.gradle (Project: AndroidMapDemo)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } |
build.gradle (Module: App)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { applicationId "com.example.muneeb.androidmapdemo" minSdkVersion 13 targetSdkVersion 24 versionCode 1 versionName "1.0" multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.google.android.gms:play-services:9.2.1' compile 'com.google.android.gms:play-services-auth:9.2.1' } |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.muneeb.androidmapdemo" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzPodyDbjhjdskZouqnHkJ7r7eRBycWGTa"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> </application> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> </manifest> |
ActivityMain.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.example.muneeb.androidmapdemo; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
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!