Google Rolls Out Android Maps API v2

Note: Version 1 of the Google Maps Android API as been officially deprecated as of December 3rd, 2012. This means that from March 3rd, 2013 you will no longer be able to request an API key for this version. No new features will be added to Google Maps Android API v1. However, apps using v1 will continue to work on devices. Existing and new developers are encouraged to use Google Maps Android API v2.

Google Maps Android API v2

Creating a new Android application that uses the Google Maps Android API v2 requires several steps. Many of the steps outlined in this section will only have to be performed once, but some of the information will be a handy reference for future applications.

Steps to be followed

Step 1 :

Download and configure the Google Play services SDK. The Google Maps Android API is distributed as part of this SDK.
i.e. The Google Play services SDK is an extension to the Android SDK and is available to you as a downloadable package from the SDK Manager. The download includes the client library and code samples.

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.
You can develop on any compatible Android device that runs Android 2.2 or higher and includes the Google Play Store.

To install the Google Play services SDK for development:
a) From Eclipse launch the SDK manager (Window > Android SDK Manager.)

b) Scroll to the bottom of the package list, select Extras > Google Play services, and install it.
– The Google Play services SDK is saved in your Android SDK environment at your-android-sdk-folder/extras/google/google_play_services/

c) Copy the google-play-services_lib library project from our-android-sdk-folder/extras/google/google_play_services/libproject/ into the source tree(i.e. Workspace) where you maintain your Android app projects.
If you are using Eclipse, import the library project into your workspace. Click File > Import , select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.

To set up a project to use the Google Play services SDK:
1. Reference the library project in your Android project.
i.e. right click on project> properties > android > add > From the list of available library projects, select a project and click OK > apply.

2. If you are using ProGuard, add the following lines in the project_directory/proguard-project.txt file to prevent ProGuard from stripping away required classes:

[sourcecode language=”java”]
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
[/sourcecode]

Step 2:

Obtain an API key. To do this, you will need to register a project in the Google APIs Console, and get a signing certificate for your app.

Note: The Google Maps Android API v2 uses

a new system of managing keys. Existing keys from a Google Maps Android v1 application, commonly known as MapView, will not work with the v2 API.

To access the Google Maps servers with the Maps API, you have to add a Maps API key to your application. The key is free, you can use it with any of your applications that call the Maps API, and it supports an unlimited number of users. You obtain a Maps API key from the Google APIs Console by providing your application’s signing certificate and its package name. Once you have the key, you add it to your application by adding an element to your application’s manifest file AndroidManifest.xml.

Displaying the certificate fingerprint(SHA1):

On Windows through command prompt go to folder C:Program FilesJavajre7bin
Then use the following command to retrieve SHA1 fingerprint.
[sourcecode language=”java”]
keytool.exe -list -v -alias androiddebugkey -keystore “C:Users[USER NAME].androiddebug.keystore” -storepass android -keypass android [/sourcecode]

e.g. In my case the would be:
[sourcecode language=”java”]
C:Program FilesJavajre7bin> keytool.exe -list -v -alias androiddebugkey -keystore “C:UsersVikas.androiddebug.keystore” -storepass android -keypass android
[/sourcecode]

you should see the display that is similar to this:
[sourcecode language=”java”]
Alias name: androiddebugkey
Creation date: Sep 12, 2012
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 26be7f47
Valid from: Wed Sep 12 10:38:51 IST 2012 until: Fri Sep 05 10:38:51 IST 2042
Certificate fingerprints:
MD5: AB:48:9B:38:5E:64:6A:1A:A9:BF:24:BD:0B:11:C8:69
SHA1: 59:3F:88:AD:01:66:CD:A9:54:A9:BA:1D:9A:BE:81:46:F4:2D:47:4E
SHA256: B3:1D:A7:C4:45:2F:25:4B:80:BB:92:34:A6:D7:94:BE:F4:74:B8:35:87:
1C:83:77:83:F2:A0:A1:9D:85:98:C5
Signature algorithm name: SHA256withRSA
Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: B5 18 AA 02 C6 C1 CD 85 03 3E 43 78 30 CE 04 52 ………>Cx0..R
0010: 55 5D FC F6 U]..
]
]
[/sourcecode]

Creating an API Project
Once you have your signing certificate fingerprint(SHA1), create or modify a project for your application in the Google APIs Console and register for the Maps API.

To get a project and register for the API:
1. In a browser, navigate to the Google APIs Console.

–> If you haven’t used the Google APIs Console before, you’re prompted to create a project that you use to track your usage of the Google Maps Android API. Click Create Project; the Console creates a new project called “API Project“. On the next page, this name appears in the upper left hand corner. To rename or otherwise manage the project, click on its name.

–> If you’re already using the Google APIs Console, you will immediately see a list of your existing projects and the available services. It’s still a good idea to use a new project for Google Maps Android API, so select the project name in the upper left hand corner and then click Create.

2. You should see a list of APIs and services in the main window. If you don’t, select Services from the left navigation bar.

3) In the list of services displayed in the center of the page, scroll down until you see Google Maps Android API v2. To the right of the entry, click the switch indicator so that it is on.

Note: Carefully see you need to ON service for “Google Maps Android API v2” not for “Google Maps API v2” 🙂 This happens with me that’s why notifying you. 🙂

4) This displays the Google Maps Android API Terms of Service. If you agree to the terms of service, click the checkbox below the terms of service, then click Accept. This returns you to the list of APIs and services.

You’re now ready to get a Maps API key.

Obtaining an API Key
If your application is registered with the Google Maps Android API v2 service, then you can request an API key. It’s possible to register more than one key per project.

To get the key

1) In the left navigation bar, click API Access.
2) In the resulting page, click Create New Android Key….
3) In the resulting dialog, enter the SHA-1 fingerprint, then a semicolon, then your application’s package name. For example:
[sourcecode language=”java”]
59:3F:88:AD:01:66:CD:A9:54:A9:BA:1D:9A:BE:81:46:F4:2D:47:4E;com.mobisys.android.google_map_ver2
[/sourcecode]

4) The Google APIs Console responds by displaying Key for Android apps (with certificates) followed by a forty-character API key, for example:
[sourcecode language=”java”]
AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
[/sourcecode]

5) Copy this key value. You will use it in the next step.

Adding the API Key to your application
The final step is to add the API key to your application. It goes in your application’s manifest, contained in the file AndroidManifest.xml. From there, the Maps API reads the key value and passes it to the Google Maps server, which then confirms that you have access to Google Maps data.

To add the key to your application:

1. In AndroidManifest.xml, add the following element as a child of the <application> element, by inserting it just before the closing tag </application>

[sourcecode language=”java”]

[/sourcecode]
substituting your API key for your_api_key. This element sets the key com.google.android.maps.v2.API_KEY to the value your_api_key and makes the API key visible to any MapFragment or SupportMapFragment in your application.

2. Add the following elements to your manifest. Replace com.example.mapdemo with the package name of your application.

[sourcecode language=”java”]

[/sourcecode]

3. Save AndroidManifest.xml and re-build your application.

Step 3:

Specify settings in the Application Manifest.

[sourcecode language=”java”]





[/sourcecode]
Also Requiring OpenGL ES version 2 permission

[sourcecode language=”java”]

[/sourcecode]

Step 4:

Add a map

After you’ve added
a) References to the Google Play services SDK
b) Added your key and
c) Customized your Android Manifest
now you can try adding a map to your application.

Please note that the code below is only useful for testing your settings in an application targeting Android API 12 or later.

1. In main.xml, add the following fragment.

[sourcecode language=”java”]


[/sourcecode]

2. In MainActivity.java, add the following code.

[sourcecode language=”java”]
package com.example.mapdemo;

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.main);
}
}

[/sourcecode]

3. Build and run your application. You should see a map. If you don’t see a map, confirm that you’ve completed all of the steps appearing earlier in this tutorial.

For the Applications that are targeting API 8 and above use the following code:
1) In main.xml, add the following fragment.

[sourcecode language=”java”]

[/sourcecode]
Note the above field class, we have used SupportMapFragment. 🙂

2. In MainActivity.java, add the following code.

[sourcecode language=”java”]
package com.example.mapdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MapsActivity extends FragmentActivity {

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

}
[/sourcecode]
Note here MainActivity extending FragmentActivity 🙂

3. Run the application, it must show the map.

Enjoy Map API Ver2… 🙂 😀

Improvements in Google Map Android API ver2:
1) More dynamic and flexible UI designs for large screen Android devices, such as tablets, using Android Fragments. So now you need not have extend MapActivity if you are using fragment.
2) Maps are now 3D.
3) You can rotate the map smoothly in any direction you want.
4) If you closely watch Map v1 API, map used to over-zoom while zooming-in or zooming-out. In Map v2, it is solved and have really good user look-and-feel when using Maps.