Monday, December 20, 2010

How to obtain the Google Maps API key for Android

If you are developing on Windows with Eclipse, an API key needs to be obtained from Google if you want to embed Google's MapView control in your custom Android mobile phone app. An example application is shown in the figure below.

Assuming the preferences for the Eclipse development platform have not been changed from the default values, then following steps can be used.

Generate the Certificate Fingerprint 
  1. On Windows, open up a Command Prompt.
  2. In the Command Prompt window, use the cd command to change to the .android subdirectory underneath your "home" directory, e.g. C:\Documents and Settings\xxxx\ where xxxx is your user name.

    C:\> cd "\Documents and Settings\xxxx\.android"
  3. Type in the following keytool command.

    C:\> keytool -list -keystore debug.keystore

    Note: if the system PATH is not set with the Java Development Kit's bin folder, then the full path to the keytool executable in the JDK/bin/ directory need to be used. For example,

    C:\> "\Program Files\jdk1.6.0_18\bin\keytool -list -keystore debug.keystore

    The following prompt is displayed.

    Enter keystore password:
  4. Type in a password, e.g. android.

    The following information may be displayed.
    Keystore type: JKS
    Keystore provider: SUN

    Your keystore contains 1 entry

    androiddebugkey, Feb 2, 2010, PrivateKeyEntry,
    Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98
  5. Note down the certificate fingerprint, e.g.  94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98.
Generate the API key
  1. Click this link to http://code.google.com/intl/ja/android/maps-api-signup.html
  2. Type in or paste the certificate fingerprint, accept the terms and conditions and click Generate API Key.

    The API key is generated.
  3. Copy this API key and use it in any Android layout.xml file that uses the MapView control.
An example layout.xml file is shown below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
  <com.google.android.maps.MapView
   android:id="@+id/map_view"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:enabled="true"
   android:clickable="true"
   android:apiKey="0Prelr1y0-DbJXwq7tIfLRwDLWTZb-MJA76FtYw"
  />
</LinearLayout>

No comments: