February 24, 2021
Hot Topics:

Mapping with Google APIs in Android, Page 3

  • Updated:
  • By Chunyen Liu
  • Send Email »
  • More Articles »

Retrieving Info in Current MapView

When a MapView is successfully created, lots of info pertaining to the current view can be queried. Here is a list of the main functions to help you better understand the current map status.

  • getMapCenter: Returns the longitude and latitude of the map center point
  • getLatitudeSpan and getLongitudeSpan: Return the span sizes of the current view's bounding rectangle
  • getZoomLevel and getMaxZoomLevel: Return the zoom level. Each zoom level is scaled by a factor of 2.
  • isSatellite: Checks whether the map view is currently in satellite imagery mode
  • isTraffic: Checks whether the map view is displaying traffic info
  • isStreetView: Checks whether the map view is currently in street-view mode

Displaying Your Info on MapView

The base class ItemizedOverlay represents an overlay that can be used to draw and display custom graphic objects on top of a map. Your code basically overrides the draw method by drawing a marker for the default position as well as enclosing its caption within a transparent rounded rectangle. Note how the point position is converted into screen coordinates with Projection.toPixels, how the font metrics is calculated with Paint's measureText, and the anti-aliased text is drawn with Paint's setAntiAlias.

private class MyItemizedOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlayItems = new ArrayList<OverlayItem>();

private Drawable marker = null;


public MyItemizedOverlay(Drawable marker) {


this.marker = marker;




protected OverlayItem createItem(int i) {




public void addOverlay(OverlayItem oi) {






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

super.draw(canvas, mapView, shadow);




Projection projection = mapView.getProjection();


// Draw point caption and its bounding rectangle

Paint p = new Paint();

p.setARGB(255, 0, 0, 255);



for (int i = 0; i < size(); i++) {

Point point = projection.toPixels(mOverlayItems.get(i).getPoint(), null);

int sw = (int)(p.measureText(mOverlayItems.get(i).getTitle()) + 0.5f);

int sh = 20;

int sx = point.x + marker.getIntrinsicWidth() / 2;

int sy = point.y - sh - 2;

RectF rec = new RectF(sx, sy, sx + sw + 10, sy + sh);



p.setARGB(80, 0, 128, 0);

canvas.drawRoundRect(rec, 7, 7, p);


p.setARGB(255, 255, 255, 255);

canvas.drawRoundRect(rec, 7, 7, p);

canvas.drawText(mOverlayItems.get(i).getTitle(), sx + 5, sy + sh - 5, p);





public int size() {




The overlay we just created needs to be added to MapView's overlay list before it goes into effect. Here is the code segment showing how you set up the overlay controller.

mMyItemizedOverlay = new MyItemizedOverlay(marker);



Figure 4 shows a label overlay on the map.

Click here for larger image

Figure 4. Label Overlay on the Map

Page 3 of 5

This article was originally published on February 15, 2012

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date