Monday, October 27, 2008

Android Map Viewer

1. Create MapViewer Project
- Create MapViewer class that extends MapActivity


import android.os.Bundle;
import android.view.KeyEvent;

public class MapViewer extends MapActivity {
private MapView map;
private MapController mc;
private static int zoomValue = 1;

public void onCreate(Bundle savedInstanceState) {
map = (MapView) findViewById(;
mc = map.getController();

public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
zoomValue = zoomValue + 1;
} else if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
zoomValue = zoomValue - 1;
} else if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
} else if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
return super.dispatchKeyEvent(event);

protected boolean isRouteDisplayed() {
return false;

2. Modify main.xml

3. Set the required permissions in the AndroidManifest.xml

4. Clean, Build and Run the Project. You will see an Empty Map Grid instead of a google map.

4. The reason why the code does not work is that the apiKey needs to be a valid key from google. Below are the steps to obtain a valid key.

- Obtaining a Maps API Key

- Edit the main.xml and replace apiKey with the value from google. See below on how to generate an MD5 Certificate fingerprint.

5. Now clean, build and compile again. Click the UP/Down to ZOOM, and LEFT/RIGHT to switch Map View and Satellite View.

6. Now that I got Map View to work, maybe I'll create something useful next time.

Source Code


Hidayath said...

Hi Padawan

It's a very nice tutorial. I am using Android SDK ver 1.1_r1
but the map is not shown. Instead only gray grid lines are shown. I have my android maps API key. I don't know how to check the validity of the key.


java.padawan said...

Sorry, the link is outdated.


Anonymous said...

Great tutorial; however I am having the same problem as Hidayath.

I have a valid maps API Key but I only get the grid lines, suggestions? also, the "" link is broken.



java.padawan said...

Sorry about this. The link keeps changing.

And I haven't had time lately to update my posts.

Here's what the new link I found in the android site.

Based from the link, it looks like I have to update this post.

Arindam Nath said...

hey i was going through ur tutorial which was really very helpful...but i need some help...i am trying to develop an app to measure small steps...but cant fix my mind on which will be the most efficient way to do it..gps..accelerometer..inbuilt compass..the main aim of the app is to detect the smallest movement and it s direction..can u suggest anything??? ~ Arindam Nath

java.padawan said...

GPS is unreliable, I don't have experience with accelerometer yet, but I think that is the way to go.