Skip to main content

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.

Comments

Popular posts from this blog

Android Swipe Views with Tabs

In this post we are going to learn about how to integrate the android tab view with the fragments using ViewPager and ActionBar class. For displaying the tabs on the top of the screen you need to interact with the android action bar, this is because the tab views is connected with the action bar. Ads by Google In this example application we make three tabs called "java", "php" and ".Net" and there are three seperate fragement view for each of these tabs. First you need to add the ViewPager into the activity_main.xml file. <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> Now create three layout files for the fragments. 1. java_layout.xml <?xml version="1.0" encod

Android MySQL Database Operations

In this post we are going to learn about how to connect to MySQL Database from your Android Application and perform database operations. Here we create an android app that contain some login Activity through which the user can retrieve information from database and a registration Activity through which the user can add information into the database.  First you need to have the following components installed in your development machine.  1. Database : Here we use the MySQL database. 2. Web Server : Here we use the Apache Web Server. 3. Server side Scripting Language :   Here we use PHP for server side scripting. 4. Android Development environment : You must install android sdk and android studio.   I recommend you to download and install WAMPSERVER. The wamp server installer contains the following components. Apache Server Application MySQL Database PHP/phpMyAdmin First we have to create the database and table in MySQL. You can use the phpMyAdmin for mange yo

"please correct the errors on this form" adsense error simple solution

Many of the bloggers now facing the problem with their adsense widgets. Whenever try to add the new  adsense link unit using the blogger widgets, it shows the error " please correct the errors on this form " as shown bellow. In blogger you can add the adsense units in two ways. First one is adding the adsense using the blogger widgets(Most of the beginners doing this) and the second method is to obtain the adsense code from the adsense login page and place it into the targeted position on the blog. To improve the revenue from adsense you have to place the appropriate ad unit into the right position. The adsense link units are very important for increasing the adsense revenue. So due to this error many of the bloggers are unable to place the link units.  Here is the solution for the problem. 1. Login into your adsense and blogger account. 2. Now make sure that only two adsense widgets (units) are present on your blog, this is because Google allows onl