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.

 

20 Comments - Average Rating:4.2

Comments:
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


if keytool is not recognized, this means the keytool file is not found in that directory
Rating: 4
Date Posted: January 19th, 2012


if we get the md5 certificate and type command in cmd then give error teytool is not reconized internal and external command.
please help me
Rating: 3
Date Posted: January 16th, 2012


its just so simple as you said..thanks for sharing..was dying for that key from 2 days..!your post was really helpful..
Rating: 5
Date Posted: January 7th, 2012


If it says that the keytool isn't recognized, you need to include the full path to the keytool.
Rating: 5
Date Posted: October 20th, 2011


when i enter "keytool -list -alias androiddebugkey -storepass android -keypass android -key
store debug.keystore"
cmd said keytool is not a recognized as internal or external commad i cand genarate MD5 fingerprint
Rating: 1
Date Posted: September 28th, 2011


This is working on both of my Google Nexus S devices w/my debug cert. - but on a Samsung Galaxy Tab and on a Samsung 510 the map view is loaded, but then I see white squares at every other tile location place *over* the map that was loaded. (This is different than the all-white view when the Google maps API key is not correct.)
Any idea?
Do I need to private/release key if I'm not testing on a Google phone?
Thanks
Rating: 4
Date Posted: September 20th, 2011


thanx buddy that helped
Rating: 5
Date Posted: September 20th, 2011




RECENT ARTICLES