Working with Google Maps Android API v2

To create a project with Google Maps Android API v2 requires several steps of configuration.


Step 1: Installing Google Play services

First of all open Android SDK Manager and check that you have installed Google APIs and Google Play services. If not please install those libraries.

Android SDK Manager

Android SDK Manager

Step 2: Obtain an API key

Open command prompt and go to your jdk/jre bin directory
In my case-

cd C:\Program Files\Java\jre7\bin

Now type the following-

keytool.exe -genkey -v -list -alias androiddebugkey -keystore "C:\users\user\.android\debug.keystore" -storepass android -keypass android

You will see something like the following generated output.

Alias name: androiddebugkey
Creation date: Jul 16, 2012
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 50041707
Valid from: Mon Jul 16 19:28:39 ALMT 2012 until: Wed Jul 09 19:28:39 ALMT 2042
Certificate fingerprints:
         MD5:  6B:68:C3:7C:95:ED:1B:CC:5D:A5:0B:AA:5A:A7:93:17
         SHA1: 88:21:38:37:86:E9:A5:25:3A:9F:40:3A:51:5E:D3:7E:CA:0C:0B:26
         SHA256: 44:B2:AB:C3:5D:54:A7:D0:53:71:65:40:21:2A:15:11:8A:7E:45:4A:33:
0B:54:66:C4:31:88:A5:19:7C:58:09
         Signature algorithm name: SHA1withRSA
         Version: 3

C:\Program Files\Java\jre7\bin>

Make sure that the Signature algorithm name: SHA1withRSA. We are going to use the SHA1: 88:21:38:37:86:E9:A5:25:3A:9F:40:3A:51:5E:D3:7E:CA:0C:0B:26 key.

Now open your browser and go to Google APIs Console. You have to login there with your gmail username and password so you must have a gmail account.
Here you will be prompted to registered your application with the Google Maps Android API v2 service. After that you can request an API key. It’s possible to register more than one key per project.

Now click the Services menu from the left navigation menu, scroll down to Google Maps Android API v2 and click the switch indicator so that it is on.

console_service
Now you are able to create New Android Key for your project.
You will find here details about how to Obtaining an API Key.

Step 3: Before creating your project

Before creating your own project copy the google-play-services_lib library project from …/your android-sdk folder/extras/google/google_play_services to your workspace.
To do this Click File > Import , select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.

Import Google play service library
Here you can see that how I have already imported the library. It is important to click the Copy project into workspace. Now you are ready to create your own project.

Step 4: Creating your Google Maps API v2 project

Now Create an Android project with eclipse with all default properties using Google APIs and API level 8.
Change your activity_main.xml file as follows.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>

And here is the MainActivity.java file.

package com.bari.hellogooglemaps;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

Your Manifest file requires several configuration. Here is a complete example.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bari.hellogooglemaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
<!-- OpenGL ES version 2 permission -->
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
<!--Replace com.bari.hellogooglemaps with the package name of your application.-->
<permission
        android:name="com.bari.hellogooglemaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.bari.hellogooglemaps.permission.MAPS_RECEIVE" />



    <!-- Allow to connect with internet -->
 <uses-permission android:name="android.permission.INTERNET" />
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.bari.hellogooglemaps.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" />
    </application>

</manifest>

Final Step

Right click on project> properties > android > add > From the list of available library projects, select a project and click OK. This is the reference of Google play service library project in your Android project.

play_service_ref

This will make your project structure look like the following.

project_structure_maps

Congrats.  Big SmileWink, you are ready to run your project.
Note: Google Play services is not supported on the Android emulator — to develop using the APIs, you need to provide a development device such as an Android phone or tablet (more details here). I made this possible with my HTC Incredible S.

Output:

Google Maps Android API v2 example

Google Maps Android API v2 example

If you found this post helpful don’t forget to comment.

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

6,256` Total Views

2 comments

Leave a Reply

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


six − = 2