Working with maps

Write App Reviews

Build your own Android App Dev Empire

Get Instant Access

We have demonstrated the start of the MapViewActivity our Wind and Waves application will use in the previous sections. There we covered the supporting classes and the handling of registering to receive location updates. With that structure in place, we now will focus on the map details themselves.

The MapViewActivity screen will look like the screen shot in figure 11.6, where several map Overlay classes are used on top of a MapView within a MapActivity.

In order to use the android.maps package on the Android platform and to support all the concepts related to a MapView, we are required to use a MapActivity.

11.3.1 Extending MapActivity

A MapActivity is the gateway to the Android Google Maps-like API package and other useful map-related utilities. There are several details behind creating and using a MapView that we as developers are fortunate enough not to have to worry about, because Map-Activity handles them for us.

You will learn more about MapView, which is what we really care about as developers building map applications, in the next section, but it's important to first understand what a MapActivity is and why it's necessary. At its core, a Map-Activity supports a MapView (a MapActivity is the only place a MapView can be used) and manages all the network and file system-intensive setup and teardown tasks needed for supporting the same.

The MapActivity onResume method

Figure 11.6 The MapViewActivity from the Wind and Waves application showing a

MapActivity with MapView

Figure 11.6 The MapViewActivity from the Wind and Waves application showing a

MapActivity with MapView automatically sets up network threads for various map-related tasks and caches map section tile data on the filesystem, for example, and the onPause method cleans these up. Without this class, all of these details would be extra housekeeping that any Activity wishing to include a MapView would have to repeat each time.

There isn't a lot you will need to do with regard to MapActivity in code. Extending this class (as we did in listing 11.3), making sure to use only one instance per process (use more than one and unexpected results may occur), and including a special manifest element to enable the package are all you need. You may have noticed the curious uses-library element in the Wind and Waves manifest in listing 11.4.

<uses-library android:name="" />

The package, where MapActivity, MapView, MapController, and other related classes such as GeoPoint and Overlay reside, is "not a standard package in the Android library" per the documentation. This manifest element is required to pull in support for the maps package.

Once you have the uses-library element and have a basic Activity that extends MapActivity, the details come inside the MapView and related Overlay classes.

11.3.2 Using a MapView

A MapView is a miniature version of many of the Google Maps API concepts in the form of a View for your Android application. A MapView displays tiles of a map, which it obtains over the network as the map is moved and zoomed, much like the web version of Google Maps.

Many of the concepts from the standard Google Maps API are also present in Android through the MapView. For instance, MapView supports a plain map mode, a satellite mode, a street-view mode, and a traffic mode. When you want to write something on top of the map, from a straight line between two points to "pushpin" markers, or full-on images or anything else, you use an Overlay.

Examples of several of these concepts can be seen in the MapViewActivity screen shots for the Wind and Waves application, such as what is shown in figure 11.6. That same MapViewActivity is shown again in figure 11.7, switched into satellite mode and zoomed in several levels.

The package supports a good many of the Google Maps API concepts but isn't identical. You have already seen the MapView we will use for the Wind and Waves application declared and instantiated in listing 11.3. Here we will discuss the use of this class inside our Activity to control, position, zoom, populate, and overlay our map.

Before we can use a map at all, we have to get a Google Maps API key and declare it in our layout file. Listing 11.6 shows the MapActivity layout file we are using with a special android:apiKey attribute.

Figure 11.7 The MapViewActivity from the Wind and Waves application using satellite mode and zoomed in on a position near Los Angeles

Listing 11.6 A MapView layout file including the Google Maps API key

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

<RelativeLayout xmlns:android="" android:layout_width="fill_parent" android: layout_height= "fill_parent"

android:gravity="center_horizontal" android:padding="10px">

< <—O Define MapView element in XML android: id= "@+id/map_view" android:layout_width= "fill_parent" android: layout_height= "fill_parent" android: enabled= "'true" android: clickable= "'true"

"051Sygx-ttd-J5GXfsIB-dlpNtggca4I4DMyVqQ" />

<LinearLayout android:id="@+id/zoom" android:layout_width="wrap_content" android:layout_height="wrap_content" android: layout_alignParentBottom= "'true" android:layout_centerInParent="true"> </LinearLayout >


A MapView can be declared in XML just like other View components O. In order to use the Google Maps network resources a MapView requires an API key Q. You can obtain a map key via a special Google Maps Android key registration web page: http: //

Before you can register for a key, you need to get the MD5 fingerprint of the certificate that is used to sign your application. This sounds tricky, but it's really very simple. When you are working with the Android Emulator, the SDK has a Debug Certificate that is always in use. To get the MD5 fingerprint for this certificate, you can use the following command (on Mac and Linux; on Windows adjust for the user's home directory and the slashes):

cd ~/.android keytool -list -keystore ./debug.keys tore -storepass android -keypass android

Getting a key for a production application involves the same process, but you need to use the actual certificate your APK file is signed with (rather than the debug.keystore file). The Android documentation has a good deal of additional information about obtaining a maps key ( html). For more information about digital signatures, keys, and signing in general, see appendix B.

Once you have a MapActivity with a MapView and have set up your view in the layout file, complete with map key, you can make full use of the map. Several of the listings we have shown up to this point are using the MapView we have declared in the Wind and Waves application. In listing 11.7 we are repeating a few of the map-related lines of code we have already shown, and we are bringing in additional related items to consolidate all the map-related concepts in one listing.



. . . from onStart this.mapController = this.mapView.getController(); this.mapController.setZoom(10); < this.mapController.animateTo(lastKnownPoint); <-.


. . . from onStart this.mapController = this.mapView.getController(); this.mapController.setZoom(10); < this.mapController.animateTo(lastKnownPoint); <-.

I Get zoom controls Q from MapView

Animate to | given GeoPoint G

| Get O MapController

. . . from onMenultemSelected case MapViewActivity. MENU_SET_MAP: this.mapView.setSatellite(false); break;

case MapViewActivity. MENU_SET_SATELLITE:

this.mapView.setSatellite(true); H


case MapViewActivity. MENU_BUOYS_FROM_MAP_CENTER: this.getBuoyData(this.mapView.getMapCenter()); break;

Set initial zoom level

Set map satellite mode

I Get coordinates <_I from map center

MapView is a ViewGroup, and you can declare it in XML and inflate it just like other view components O. Because it is a ViewGroup you can also combine and attach other elements to it. Beyond the MapView itself, we are using a separate additional View-Group for the zoom controls Q and attaching the controls from the map to it Q.

Next we get a MapController from the MapView Q, and we use the controller to set the initial zoom level Q and animate to a specified GeoPoint O. The controller is what you use to zoom and move the map. Also, when the user chooses to do so via the menu, we set the mode of the map from plain to satellite and vice versa Q. Along with manipulating the map itself, we can get data back from it, such as the coordinates of the map center I.

Above and beyond manipulating the map and getting data from the map, you also have the ability to draw items on top of the map using any number of Overlay instances.

11.3.3 Placing data on a map with an Overlay

The small buoy icons on the MapViewActivity for the Wind and Waves application that we have used in several figures up to this point are drawn on the screen at specified coordinates using an Overlay.

Overlay is a generalized base class for different specialized implementations. You can roll your own Overlay by extending the class, or you can use the included MyLocationOverlay. The MyLocationOverlay class lets you display a user's current location with a compass, and it has other useful features like including a Location-Listener for convenient access to position updates.

Another common use case for a map (in addition to showing you where you are) is the need to place multiple marker items on it—the ubiquitous pushpins. We have this exact requirement for the Wind and Waves application. We need to create buoy markers for the location of every buoy using data we get back from the NDBC feeds. Android provides built-in support for this with the ItemizedOverlay base class and the Overlayltem.

An Overlayltem is a simple bean that includes a title, a text snippet, a drawable marker, and coordinates using a GeoPoint (and a few other properties, but you get the idea). Listing 11.8 is the buoy data-related BuoyOverlayltem class that we are using for Wind and Waves.

Was this article helpful?

0 0


  • lidya
    How to use a single MapView across several Map Activities android?
    8 years ago

Post a comment