ff

Recent Posts

Sunday, September 8, 2013

Google Maps API V2 for Android



The Older Maps API V1 is officially deprecated as of December 3rd 2012. Starting from March 3rd you will no longer able to apply maps api key obtained using Maps API V1. This Tutorial is about how to use Google Maps API V2 for Android application development.  This Tutorial have described on both Android Studio and Eclipse IDEs.
 

Android Studio


 
It is easy to get the eclipse folder structure to avoid gradle errors on Android Studio. So lets create a project using eclipse and later import it to Android Studio
Create new Project using eclipse IDE
Go to File à New à Other


 

You don’t want to consider the build and compile sdk versions bcoz we will change them later.

Then click next,,, next,,, next,,, and finally “finish”

Then open Android Studio and go to Fileà Import Project and browse and open your previously created eclipse project



Make shure to select Google API as Build target (shown in following image)



Click next and Finish import.

Initially you should manually download and install the Google Play Services using SDK Manager .

Open the SDK manager by going  Tools à Android à SDK Manager
 


Then SDK Manager will be open , then scroll to bottom of the window and you will find “Extras” section. There is “Google Play Services” check box and on default it will not installed initially. If so you should check it and Click on Install Package Button.(Note: in the following image it is already installed for me.)



After you installed it, you should import the Google Play Services Library to your work space. To do this

Go to  File à Project Structure or press “Ctrl + Alt + Shift + S” and Open the Project Structure Dialog.

 
 
Then Select the Module  and click on the green plus button appear in the left top of the middle section as shown on following image. Then select the Import Module from drop down list.
 


Open the following url by browsing  ~\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib

You can find this for the SDK comes with Android Studio in

C:\Users\<UserName>\AppData\Local\Android\android-studio\sdk\extras\google\google_play_services\libproject\google-play-services_lib

Note: If you cant see the “AppData” folder in browse window just click on “show hidden folders” button

Click OK button

 


Click on Next button for next few dialog boxes and finally click Finish. Now you can See “google play services lib” project has imported.


Now Select on your Project name, under module section and click on the green plus button appears in right side under the “dependencies” tab and click on “Module Dependency”



Then Select “google-play-services-lib” on the dialog box

 



Again click on the green plus button and click “jars or directory” and import the google-play-services.jar file located under C:\Users\<UserName>\AppData\Local\Android\android-studio \sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs\google-play-services.jar




Use the up/down arrows to move <Module source> to the bottom of the list.

 

Then our next step is to Display the Google Map on Android device. In Map V2 your android version api level must be greater than or equal to 11. Otherwise it will not support.

 

Your next step is to take a Maps API key to uniquely identify your project, before having Maps API key you must have sign your certificate. This is based on SHA-1 fingerprint. The fingerprint is a unique text string generated from the commonly-used SHA-1 hashing algorithm. Because the fingerprint is itself unique. There are two types of certificates

1.Debug Certificate

This certificate only allow you to test your application, you can’t publish it to google play store. It  will have an expiration date of 365 days from its creation date.

           

2.Release Certificate

 

In this tutorial I only use Debug certificate to demonstrate the tutorial.

How to obtain a Debug Certificate from Command Line

Debug keystore can be found at <userhome/.android/debug.keystore>

To generate SHA-1 of debug keystore use below command by changing current directory to “.android” directory in Command line.



 

keytool -list -v -keystore <debug keystore path>/debug.keystore –alias


 androiddebugkey -storepass android -keypass android
 


To access the Google Maps with the Maps API, you have to add a Maps API key to your google application project. You can get this key via Google API Console .

Create your project in Google API Console. Go to following link.


if this is the first time you enter to the google api console, you will prompt to asking for create new project. Click on “Create Project” then you will get All Services window.

 


Scroll to down and find “Google Maps Android API v2” and toggle on the button appears in front of the text by accepting google terms of services agreement.
 
 

 
Then go to “API Access” tab and click on “Create new Android key” button.



Type your obtained debug key and android project package name with separated by a comma.

Click on “Create” button.

Note: In this example I have used “com.example.myapplication” as my package name, if you have different one, use your own package name.



Copy and Save the “API Key” somewhere on your local computer, you will need this later.
Now you are ready for Display your Map. Go to Android Studio Project and add the following code to your layout.
<fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
 
In Order to Display Your map using “MapFragment” class, you must have some permissions add  on your “AndroidManifest.xml” file and you also add your  google map api key as meta deta.
Note:
1.       <metadata> element must be place as a child of <application> element and before the closing tab of application element
2.      <uses-permission> elements must place as child of <manifest> element
·         android.permission.INTERNET Used by the API to download map tiles from Google Maps servers.
·         android.permission.ACCESS_NETWORK_STATE Allows the API to check the connection status in order to determine whether data can be downloaded.
·         com.google.android.providers.gsf.permission.READ_GSERVICES Allows the API to access Google web-based services.
·         android.permission.WRITE_EXTERNAL_STORAGE Allows the API to cache map tile data in the device's external storage area.
The following permissions are recommended, but can be ignored if your application does not access the user's current location, either programmatically, or by enabling the My Location layer.
·         android.permission.ACCESS_COARSE_LOCATION Allows the API to use WiFi or mobile cell data (or both) to determine the device's location.
·         android.permission.ACCESS_FINE_LOCATION Allows the API to use the Global Positioning System (GPS) to determine the device's location to within a very small area.
 
3.       version 2 of the Google Maps Android API requires OpenGL ES version 2, you must add a <uses-feature> element as a child of the <manifest>
<uses-feature

        android:glEsVersion="0x00020000"

        android:required="true"/>
    
4.      add following permissions with your package name instead of red text.
<permission
         android:name="com.example.myapplication.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
    <uses-permission android:name="com.example.myapplication.permission.MAPS_RECEIVE"/>
 
Now your AndroidManifest.xml file must appear like this..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>
 
    <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"/>
 
    <permission
            android:name="com.example.myapplication.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
    <uses-permission android:name="com.example.myapplication.permission.MAPS_RECEIVE"/>
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myapplication.MainActivity"
            android:label="@string/app_name" >
            <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.maps.v2.API_KEY"
                android:value="your api key put here"/>
 
    </application>
</manifest>
 
Then put the following code in your activity_main.xml file
Now Run your project. it will make you smile
We can do lot with maps.. Lets learn them one by one in future tutorials.
For next tutorial I’ll show you how to create this app on eclipse with google map api.



 

0 comments :

Post a Comment