Display Google Map using API V2 in android

Displaying Google Map in android using the API version 1 is deprecated now. Google introduce a lot of changes in displaying Google Map using API version 2. In this post i demonstrate how to display the Google Map using API Version 2.
Ads By Google



Step 1:
Install the Google Play Service SDK 
For displaying Map using API V2, you need the Google Play Services. So first step is to open your Android SDK manager and install the Google Play Services.

Step 2:
Add the Google Play Service as a library project into your android work-space. 
After the successful installation of Google Play Service SDK, you can find out the Google play service library project on the following location in your android SDK folder. 
                                           sdk-->Extras-->Google
Import the Google Play Services as a library project into the work-space. Don't forget to choose the options for copy the project into workspace. 
Step 3:
Add the Google play library project into your android project. 
Now you can add google play service-libraries into your project. Right click your project and choose its properties and choose the android option and click add button at the bottom of  the window. 
Step 4:
Generate the SHA-1 fingerprint Using the Java keytool. 
You need to generate the SHA-1 fingerprint using java keytool for obtaining the Map API key. Open your command prompt and type the following command. 
 keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android  
After type the above command you got the certificates figerprints. Copy the SHA-1 fingerprints.

Step 5:
Obtain the Google Map API Key using the SHA-1 Fingerprint and your application package name from the Google console. 
Login into Google console  and at the left corner of the page choose the API's under API's & auth option. Now search for Google Maps Android API V2 and change its status from off to on
Now click Credentials at the left side of the page and click the CREATE NEW KEY button. Choose Android Key option from the dialog window. Now insert your SHA-1 fingerprint and your application package name into the text area of the newly generated dialog window and click create. Now you can see the newly created API KEY for android at the top of the web page. Copy that key. 

Step 6:
Specify the Google Play Service versions in the Manifest file.
Open your android Manifest file and add new Xml tag called meta-data between the Application tag. Specify your API Key as the value for the meta-data. 
 <meta-data  
android:name="com.google.android.maps.v2.API_KEY"
android:value="Enter your API Key Here">
</meta-data>

Step 7:
Add the required permissions in manifest file
We need to specify some permissions in manifest file  for displaying the Google Map. Following are the permissions and its usage. 

android.permission.INTERNET = "used to connect to the Google Map Servers for downloading the map tiles "
android.permission.ACCESS_NETWORK_STATE= "used by the API to check the status of the connection for downloading the data"
com.google.android.providers.gsf.permission.READ_GSERVICES = "Used by the API to access Google web based services".
android.permission.WRITE_EXTERNAL_STORAGE = "Used by the API to save the Map tile data into the external storage".
android.permission.ACCESS_COARSE_LOCATION = "Used by the API to access the WiFi or mobile data to find out the device current location".
android.permission.ACCESS_FINE_LOCATION = "Used by the API to access GPS for finding the current location ".
 <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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Step 8:
Specify the OpenGL ES version in manifest. 
The google Map API V2 uses the OpenGL ES version 2 for rendering the Map on devices. 
  <uses-feature  
android:glEsVersion="0x00020000"
android:required="true"
/>

Step 9:
Add a fragment to the activity-main.xml file for display Map
 <?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"/>

Since Google Map uses the Google play services, it is not possible to test the Google map app in an android virtual device. For testing this app you need to install the .apk file in a real device.