?

Getting started with Google Maps on Android

Read this tutorial to learn how to use Google Maps on Android devices.

JP
Jim Parker

Tutorial: Getting started with Google Maps on Android

If you're looking to get started with Google Maps on Android, you've come to the right place. The goal of this tutorial is to have a working Google Map on your emulator in 30 minutes or less.

Here's what you will need for this tutorial:

1. Eclipse with Android SDK/AVD manager installed

2. Google APIs library downloaded using the SDK/AVD manager

These are the steps we will be covering:

1. creating a Google APIs Android project

2. setting permissions in the manifest

3. obtaining a Google Maps API key

4. using a MapView and a MapActivity

5. look at your Google Map

Step 1: creating a Google APIs Android project

First, if you haven't done this already, download a Google APIs library using the SDK/AVD manager and create an AVD (an Android emulator instance) that uses this library.

If you are creating a new project, then select File -> New -> Project -> Android -> Android Project. Choose a Google APIs build target and finish creating your new project. Run your new project as an Android Application and you should see the familiar "Hello World, <project name>" display on your emulator.

If you are adding the map to an existing project, click on the project and select Properties -> Android -> then select a Google APIs as your build target

Step 2: setting permissions in the manifest

We are now ready to add two important settings to the project manifest: a usage setting for the Google APIs library and an internet access permission.

The first setting will allow your application to use the Google APIs library. To edit the manifest, you can either use the built in GUI editor, or manually edit the raw xml. To add this setting using the GUI editor, go to the Application tab, click add.., Uses Library, and select com.google.android.maps in the name drop down.

To add this setting manually, add

<uses-library android:name="com.google.android.maps"/>

under the application tag.

The second setting will give your application permission to access the internet.

To add this setting using the GUI editor, go to the Permissions tab, click add.., Uses Permission, and

select android.permission.INTERNET.

To add this setting manually, add

<uses-permission android:name="android.permission.INTERNET" />

under the manifest tag.

Here is an illustration of how your manifest should look when you are done:

<manifest>

<application>

<some other stuff/>

<uses-library android:name="com.google.android.maps"/>

</application>

<some other stuff/>

<uses-permission android:name="android.permission.INTERNET" />

</manifest>

Step 3: obtaining a Google Maps API key

This is the most difficult step, and will take a little bit of time. First, you should understand that when you compile an APK (Android package) and push it to an Android device (even your emulator) it is signed with a certificate. You will need to retrieve the MD5 fingerprint from this certificate to sign up for an API key. If you google for "android google maps api key sign up" you should be able to find the Google's Map API Key sign up page without too much trouble. Their page will provide a brief explanation of this entire process.

To get started, you will need to find the certificate you are using to sign your application. If you have not added your own keystore, you are likely using a debug keystore. To see where this keystore is located, go to Eclipse -> Preferences -> Android -> Build -> and you will see the location under Default debug keystore. Once you have this information, in Linux simply run

keytool -list -keystore <your keystore location>

any names and passwords it asks for will be: android

In Windows, you will probably want to use the keytool.exe provided with Java. You may get an error if keytool is not in your Windows path. Either add it to your path or run keytool directly from the bin directory in your Java folder.

If done correctly, you should see something like:

Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98

Now, copy 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98 or whatever your MD5 fingerprint is (should be different) and paste it into google's sign up box. You will need a google account to get your key. Google will give you a success screen with your key and a code snippet that look like this-

Your key is:

0AuNnVn-pYluVNgxpSs6ZD_Cy9KZXMA-qdhbTzA

Here is an example xml layout:

<com.google.android.maps.MapView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:apiKey="0AuNnVn-pYluVNgxpSs6ZD_Cy9KZXMA-qdhbTzA"

/>

Step 4: using a MapView and a MapActivity

Now, the final step. If you created a new project for this tutorial, you can add the MapView code snippet above to the Linear Layout in your main.xml. Make sure to replace the API Key in the above snippet with your own API Key. It should look something like this:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<com.google.android.maps.MapView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:apiKey="YOUR API KEY GOES HERE"

/>

</LinearLayout>

Otherwise, simply add this MapView to any other layout like normal.

Last but not least, update the Activity displaying your MapView layout. Instead of extending Activity, your class should now extend MapActivity. That's it. You're done.

Step 5: look at your Google Map

Launch your application. You should see a map of the United States zoomed out really far. If instead you get a force close error, make sure you correctly added the Google APIs library setting in Step 2. If you only see a white grid it could be one of many things. You could be having an internet outage, you incorrectly added the internet permissions from Step 2, you have an incorrect API Key, or you forgot to use your API Key. A lot of things can go wrong here. If things didn't work out you aren't the first. Keep trying and don't give up.

If everything is working, then congratulations and have fun playing with your new Google Map on Android. There are a plethora of other tutorials online to help you take it from here!