Login     Sign Up
Tugadar (@admin)
14 days ago

Canvas drawing for Overlays is handled by overriding the Overlay’s draw handler.

The passed-in Canvas is the surface on which you draw your annotations, using the same techniques introduced in Chapter 4 when creating custom User Interfaces for Views. The Canvas object includes the methods for drawing 2D primitives on your map (including lines, text, shapes, ellipses, images, etc.). Use Paint objects to define the style and color.

The following code snippet uses a Projection to draw text and an ellipse at a given location:


public void draw(Canvas canvas, MapView mapView, boolean shadow) { Projection projection = mapView.getProjection();

Double lat = -31.960906*1E6;

Double lng = 115.844822*1E6;

GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());

if (shadow == false) {

Point myPoint = new Point();

projection.toPixels(geoPoint, myPoint);

Create and setup your paint brush Paint paint = new Paint(); paint.setARGB(250, 255, 0, 0); paint.setAntiAlias(true); paint.setFakeBoldText(true);

Create the circle

int rad = 5;

RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);

Draw on the canvas canvas.drawOval(oval, paint);

canvas.drawText(“Red Circle”, myPoint.x+rad, myPoint.y, paint);



For more advanced drawing features, check out Chapter 11, where gradients, strokes, and filters are introduced that provide powerful tools for drawing attractive and compelling map overlays.

Handling Map Tap Events

To handle map taps (user clicks), override the onTap event handler within the Overlay extension class.

The onTap handler receives two parameters:

❑ A GeoPoint that contains the latitude/longitude of the map location tapped ❑ The MapView that was tapped to trigger this event

When overriding onTap, the method should return true if it has handled a particular tap and false to let another overlay handle it, as shown in the skeleton code below:


public boolean onTap(GeoPoint point, MapView mapView) {

Perform hit test to see if this overlay is handling the click if ([... perform hit test ... ]) {

[ ... execute on tap functionality ... ] return true;


If not handled return false

return false;


Adding and Removing Overlays

Each MapView contains a list of Overlays currently displayed. You can get a reference to this list by calling getOverlays, as shown in the snippet below:

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

Adding and removing items from the list is thread safe and synchronized, so you can modify and query the list safely. Iterating over the list should still be done within a synchronization block synchro-nized on the List.

To add an Overlay onto a Map View, create a new instance of the Overlay, and add it to the list, as shown in the snippet below:

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

MyOverlay myOverlay = new MyOverlay();



The added Overlay will be displayed the next time the Map View is redrawn, so it’s usually good practice to call postInvalidate after you modify the list to update the changes on the map display.