Android - How To Set Up an API Key for Google Maps

It took me some time to figure out all the steps to get my Android
apps set up to use Google Maps.  Hopefully this will spare you some
time.  First I'll explain how the process goes when you don't have any problems.  If you're having problems, you can click here to find out about the troubles I ran into when I upgraded from SDK 1.1 to 1.5.

Open a command prompt and navigate to a file called debug.keystore. The location of this file depends on what OS you are using. I use Windows XP and my debug.keystore file is located at C:\Documents and Settings\User\.android.

As a side note, before the Android 1.5 SDK came out, my debug.keystore file was located in a different location (C:\Documents and Settings\HP_Administrator\Local Settings\Application Data\Android). When I later updated to 1.5 I ran into all sorts of problems getting the maps to work.

if you use a Mac or Linux OS, I've discovered that you'll find the debug.keystore file at: ~/.android/.
 

The debug.keystore contains a digitial certificate that android uses to launch applications on the emulator. The certificate is called 'androiddebugkey'.  We'll need to get some an MD5 hash for this certificate in order register for our map api key.


Hopefully you've located the debug.keystore file, so now we'll use Java's keytool utility to get the MD5.

I use XP so I navigated to the proper directory by opening a command prompt and typing:
cd C:\Documents and Settings\HP_Administrator\.android

Once you've navigated to the proper directory for your os, type this in...
keytool -list -alias androiddebugkey -storepass android -keypass android -key
store debug.keystore


Once you hit the enter key, you'll see something like this (the actual MD5 that we're interested in is the last line):
androiddebugkey, Mar 10, 2009, PrivateKeyEntry,
Certificate fingerprint (MD5): D1:16:4A:BD:73:73:A4:56:9D:CD:9A:44:A2:6C:11:AC


Now open your browser and go to the following url...
http://code.google.com/intl/ja/android/maps-api-signup.html

Accept the agreement and paste the md5 that the key tool returned to you, in my case it was something like...
D1:16:4A:BD:73:73:A4:56:9D:CD:9A:44:A2:6C:11:AC

Then press "Generate API Key" and you'll be redirected to a page that has your api key.

Now
that you have the api key, you can use it in the main.xml layout file
of your MapTest application. Here's what mine looks like:

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


You'll also have to add some entries to your manifest file, here's what mine looks like...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.remwebdevelopment.maptest"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <!-- this was the kicker - had to import the library com.google.android.maps!!! -->
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapTest"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <!--make sure you add the following permissions to your app!!!-->
    <uses-permission
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="android.permission.INTERNET">
    </uses-permission>
    <uses-permission
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="android.permission.ACCESS_FINE_LOCATION">
    </uses-permission>
</manifest>


Here's what my MapActivity looks like(note that it extends MapActivity, not Activiy)...

package com.remwebdevelopment.maptest;

import com.google.android.maps.MapActivity;


import android.os.Bundle;

public class MapTest extends MapActivity {

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

        //you could get a reference to the map view from the main.xml
        //layout like this...
        //mMapView = (MapView)findViewById(R.id.mapview1);
    }

    //you must provide an implementation for isRouteDisplayed()
    //when you extend MapActivity...
    @Override
    protected boolean isRouteDisplayed() {
    return false;
    }
}


You should see a map of the world when you run it.
Hopefully this will get you up and running.  I have other code samples
that might be helpful, I'll be posting them soon.

 

If you're having problems, I might be able to help you by sharing my struggle to get the maps working after I updated from the 1.1 SDK to 1.5. Click here for more info.

 

27 Comments - Average Rating:4.33

Comments:
Remember. You need two API keys. One for your Keystore and another for the android self generated debug keystore.
Rating: 5
Date Posted: April 18th, 2013


hey can plz tell me how to make the application run on a real device plz my map application works fine in emulator but map is not showing on real device
Rating: 5
Date Posted: March 31st, 2013


Easy to read, easy to understand. Thanks
Rating: 5
Date Posted: October 19th, 2012


Thanks a million...very clear explanation of something that I was not used to doing.
Rating: 5
Date Posted: August 22nd, 2012


crisp clear example,saved lots of my time.
Rating: 5
Date Posted: June 11th, 2012


Helped me easily get a key in a few minutes, thanks!
Rating: 5
Date Posted: March 10th, 2012


pls tell me what to do, if
we get the error as keytool not recognized?
Rating: 3
Date Posted: February 17th, 2012


http://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-key-for-android-issues-and-errors-solved/

this link works and solves all the issues related to generating MD5 fingerprint using keytool
Rating: 5
Date Posted: January 26th, 2012




RECENT ARTICLES