Pages

Monday, October 4, 2010

Android MapView Tutorial Part 1

Learning how to use Google Maps in your Android applications is useful and in some cases essential.  It is easy to do as this tutorial will illustrate. In part 1 we will look at getting a working MapView in our application. In part 2 we will look at how you can add overlays to your map.

Before we begin writing code for our application we need to ensure we have the correct build target called "Google APIs Add-on". For this tutorial I am using Android's latest 2.2.

If you have not done so set up an AVD that uses the same API target and remember to use this AVD when you build and run the application.

Next we will need a Google Maps API Key. You can obtain a debug key by going to this page and following the instructions under Getting the MD5 Fingerprint of the SDK Debug Certificate. For those who are wondering, the keytool they mention is usually under your JRE as part of your JDK, do a search for it.

1. Once you have obtained the Key (a long string of alphanumeric characters) start by creating a new project, HelloMapView.

2. Next modify the Android Manifest. We need to add two elements. First we need to declare the maps library in the Android Manifest under the application element like this:
<uses-library android:name="com.google.android.maps" /> 
Second, the application will require Internet access so request for INTERNET permission in the manifest file by adding the following as a child of the manifest element:
<uses-permission android:name="android.permission.INTERNET" />
3. Open the res/layout/main.xml file and add com.google.android.maps.MapView as the root node:
<?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="Your Maps API Key goes here"
/>
The android:clickable attribute holds a boolean that says whether you want to allow user-interaction with the map. The android:apiKey attribute holds the Maps API Key for your application which you obtained earlier. Replace with "Your Maps API Key goes here" with the debug key you obtained earlier.

4. Open the HelloMapView.java file. For this Activity we will extend MapActivity:
public class HelloMapView extends MapActivity {
5. The isRouteDisplayed() method is required in every MapActivity, so we override this method:
@Override
protected boolean isRouteDisplayed() {
    return false;
}

For the moment we won't be displaying any route information thus isRouteDisplayed is set to false.

6. We will leave the standard OnCreate() method as is which will load our main.xml that we defined above. We will enable the built in zoom feature of the map by adding the following to the end of the OnCreate() method:
MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
You can now run the application. You should get a working map application with zoom functions. In part 2 we will look at adding overlays to your application.

Trouble-shooting: If your application has a Force Close exception at this point it is often due to problems in your Android Manifest. Check and make sure you have placed the right element in the right place. As a rule, Permission elements is placed before the application element. If the map tiles don't show up check and make sure your key is entered in the correct place.

No comments:

Post a Comment