: Beginning Android

The Bare Bones

The Bare Bones

Far and away the simplest way to get a map into your application is to create your own subclass of MapActivity. Like ListActivity, which wraps up some of the smarts behind having an activity dominated by a ListView, MapActivity handles some of the nuances of setting up an activity dominated by a MapView.

In your layout for the MapActivity subclass, you need to add an element named, at the time of this writing, com.google.android.maps.MapView. This is the longhand way to spell out the names of widget classes, by including the full package name along with the class name. This is necessary because MapView is not in the com.google.android.widget namespace. You can give the MapView widget whatever android:id attribute value you want, plus handle all the layout details to have it render properly alongside your other widgets.

However, you do need to have:

android:apiKey, which in production will need to be a Google Maps API key more on this here

android:clickable="true", if you want users to be able to click and pan through your map

For example, from the Maps/NooYawk sample application, here is the main layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="<YOUR_API_KEY>"
android:clickable="true" />
<LinearLayout android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>

Well cover that mysterious zoom LinearLayout and the apiKey in later sections of this chapter. In addition, you will need a couple of extra things in your AndroidManifest.xml file:

The INTERNET and ACCESS_COARSE_LOCATION permissions

Inside your <application>, a <uses-library> element with android:name="com.google.android.maps", to indicate you are using one of the optional Android APIs

Here is the AndroidManifest.xml file for NooYawk:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.commonsware.android.maps">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".NooYawk" 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>

That is pretty much all you need for starters, plus to subclass your activity from MapActivity. If you were to do nothing else, and built that project and tossed it in the emulator, youd get a nice map of the world. Note, however, that MapActivity is abstract you need to implement isRouteDisplayed() to indicate if you are supplying some sort of driving directions or not.

In theory, the user could pan around the map using the directional pad. However, thats not terribly useful when the user has the whole world in her hands.

Since a map of the world is not much good by itself, we need to add a few things


: 1.063. /Cache: 3 / 1