Embedding a Map View

A MapView can be embedded directly in your Android application with just a few lines of code. Most of the functionality of Google Maps, plus hooks for adding your own touches, is provided (see Figure 8.3, on the following page).

The MapView class can also tie into your location and sensor providers. It can show your current location on the map and even display a compass showing what direction you're heading. Let's create a sample program to demonstrate a few of its capabilities.

First create a "Hello, Android" application using these values in the wizard:

Project name: MyMap

Build Target: Google APIs (Platform: 2.2)

Application name: MyMap

Package name: org.example.mymap

Create Activity: MyMap

Min SDK Version: 8

Note that we're using the "Google APIs" build target instead of the "Android 2.2" target. That's because the Google Maps APIs are not part of the normal Android distribution. Edit the layout file, and replace it with a MapView that takes over the whole screen.

6. http://maps.google.com

Figure 8.3: Embedded map showing your current location

Download MyMap/res/layout/main.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id= "@+id/frame" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView android:id="@+id/map" android:apiKey= "MapAPIKey" android:layout_width= "fill_parent" android:layout_height="fill_parent" android:clickable= "true" />

</LinearLayout>

Substitute MapAPIKey with a Google Maps API key that you get from Google.7 Note that we have to use the fully qualified name (com.google.

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

android.maps.MapView) because MapView is not a standard Android class. We also need to stick a <uses-library> tag in the <application> element of AndroidManifest.xml:

Download MyMap/AndroidManifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.example.mymap" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name= ".MyMap"

android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

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

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" /> </manifest>

If you leave out the <uses-library> tag, you will get a ClassNotFoundEx-ception at runtime.

In addition to the fine- and coarse-grained location providers, the MapView class needs Internet access so that it can call Google's servers to get the map image tiles. These will be cached in your application directory automatically.

Here's the outline of the MyMap class:

Download MyMap/src/org/example/mymap/MyMap.java package org.example.mymap;

import android.os.Bundle;

import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay;

public class MyMap extends MapActivity { private MapView map; private MapController controller;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initMapView(); initMyLocation();

@Override protected boolean isRouteDisplayed() { // Required by MapActivity return false;

The most important part is that your activity has to extend MapActivity. The MapActivity class spins up the background threads, connects to the Internet for tile data, handles caching, does animations, takes care of the life cycle, and much more. All you need to do is properly set it up and let it go.

0 0

Post a comment