Login     Sign Up
Tugadar (@admin)
14 days ago
12 Views

Overlays are a way to add annotations and click handling to MapViews. Each Overlay lets you draw 2D primitives including text, lines, images and shapes directly onto a canvas, which is then overlaid onto a Map View.

You can add several Overlays onto a single map. All the Overlays assigned to a Map View are added as layers, with newer layers potentially obscuring older ones. User clicks are passed through the stack until they are either handled by an Overlay or registered as a click on the Map View itself.

Creating New Overlays

Each overlay is a canvas with a transparent background that you can layer on top of a Map View and use to handle map touch events.

To add a new Overlay, create a new class that extends Overlay. Override the draw method to draw the annotations you want to add, and override onTap to react to user clicks (generally when the user taps an annotation added by this overlay).

The following code snippet shows the framework for creating a new Overlay that can draw annotations and handle user clicks:

import android.graphics.Canvas;

import com.google.android.maps.MapView;

import com.google.android.maps.Overlay;

public class MyOverlay extends Overlay {

@Override

public void draw(Canvas canvas, MapView mapView, boolean shadow) { if (shadow == false) {

[ ... Draw annotations on main map layer ... ]

}

else {

[ ... Draw annotations on the shadow layer ... ]

}

}

@Override

public boolean onTap(GeoPoint point, MapView mapView) {

Return true if screen tap is handled by this overlay return false;

}

}