Android - Application - Using Google Maps

As the Google Maps API v1 is now obsolete, I could recommend to check this tutorial for the new version of Google Maps API v2.

This Tuto 1 could be found on the Android Market and the direct link is on your right, in the menu.

The Android API allows us to use the Google Maps library.

So, in this tutorial, for beginners, we will create a first application with some examples of the Google Maps.

Let's see it in details:

1. Creating a new Android project

Create a new Android project in Eclipse :

Project name: BadprogGoogleMaps.
Contents: let all by default.
Build Target: Google APIs with Platform 2.1-update1  and the API Level 7.
Application name: BadprogGoogleMaps.
Package name: com.badprog.android.
Check Create Activity then write: BadprogGoogleMapsActivity.
Min SDK Version: 7.

Click Finish.

2. Generate the Google Maps API key

We will now create a valid Google Maps API key to display maps on our Android device.

Open Eclipse then :

Windows > Preferences > Android > Build

In the Default debug keystore input, you can see the path until your debug.keystore.
It is like this on Windows 7, for example:

C:\Users\Badprog\.android

You have to open a command line in this directory.

If you do not know how to do this, let's go on the Badprog directory (change Badprog by your username!).

SHIFT + Right click on the .android directory.
A window will appears, select Open command window here.

OK, so you have a new MDOS ready (this famous black window command line) and you are in the .android directory.

We have to use now the JAVA tool: keytool.

You have it with the JDK that you normally installed.
If your command line can not find it, you have to find the JAVA bin path and write this path instead of just keytool in the following example.

Let's now type some code inside our command line:

$ keytool -v -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android

Some information should appear.
What interests us is something like that:

Certificate fingerprints:
         MD5:  38:F9:9A:DD:A4:37:DF:01:07:B0:36:82:01:2D:DC:E5

Note that the MD5 code is the good one, do not take the other, such as SHA1 or SHA256.

Now let's go on Google website to create a Maps API key:

http://code.google.com/android/maps-api-signup.html

At the bottom of the page, enter your MD5 code and click Generate API key.

Of course you must have a Google account.
If you do not have one, create it with a simple email address.
Once done, the website will give you this famous API key!
It is something like that:

0FCBLi0_8mstfe4pFPMpQF1kLJpMVhtioV5SzIg

This code is unique and corresponds to your MD5 code.

3. Managing the JAVA and XML files

OK, the worst is behind us, let's modify the files in our project.

Open the BadprogGoogleMapsActivity.java file and replace all the code by this:

// src/com.badprog.android/BadprogGoogleMapsActivity.java
package com.badprog.android;

import android.os.Bundle;

import com.google.android.maps.MapActivity;

public class BadprogGoogleMapsActivity extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Then replace the res/layout/main.xml file by this (change the apiKey by yours of course):

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

And finish by the AndroidManifest.xml that we replace by:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.badprog.android"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />
    
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".BadprogGoogleMapsActivity"
                  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>
</manifest>

4. Selecting the good AVD

As a corresponding Android Virtual Device, we will set a new one:

Open the AVD Manager and set it like that:

Name: e_gmaps
Target: Google APIs (Google Inc.) - API Level 7
Skin > Built-in: HVGA

Click Create AVD.

Close this window.

5. Displaying the Google Maps

You are now ready to see the world.
Run your application and wait until you see the Google Maps service appears.

Normally you are seeing the Google Maps.
You can simple click the map to move around the world!

Good job! laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.