//
you're reading...
HCI, Tech

Introducing Overlay to Android’s MapView

WHAT

An Overlay is an imaginary layer above the MapView where it is stored in the MapView’s data structure (gettable via MapView.getOverlays()).

WHY

Why do we want to use Overlay?

1) We want to put stuff on a map (e.g., a mark, a route, text);

2) We want to get touch event before the MapView does (so that we can, say, override the pan & zoom operation).

HOW

Step 1. Create a class that extends Overlay (or its subclasses)

Here is a step-by-step tutorial. To get a holistic idea, an overlay class primarily overrides the following methods so that we can do something on this imaginary layer:

draw(...), drawAt(...), onKeyDown(...), onKeyUp(...), onTap(...), onTouchEvent(...), onTrackballEvent(...), ...

Step 2. Put that class into the MapView’s Overlay List

This usually happens in your onCreate method. After the usual initialization of the map, we first get MapView’s List of Overlays:

List<Overlay> mapOverlays = mapView.getOverlays();

We then create an instance of our Overlay class, called myOverlay; then we simply add this object to the List:

mapOverlays.add(myOverlays);

The effect of this Overlay depends on how many methods you have overridden in your Overlay class. For example, if you have overridden onTouchEvent(), then the MapView’s default pan & zoom no longer works.

Advertisements

About Xiang 'Anthony' Chen

Making an Impact in Your Life

Discussion

Trackbacks/Pingbacks

  1. Pingback: Overriding Android MapView’s Default Pan & Zoom « A Designer with Dirty Hands - December 1, 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

%d bloggers like this: