October 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Mapping with Google APIs in Android, Page 3

  • Updated: February 15, 2012
  • 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) {

super(marker);

this.marker = marker;

}

 

@Override

protected OverlayItem createItem(int i) {

return(mOverlayItems.get(i));

}

 

public void addOverlay(OverlayItem oi) {

mOverlayItems.add(oi);

populate();

}

 

@Override

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

super.draw(canvas, mapView, shadow);

 

boundCenterBottom(marker);

 

Projection projection = mapView.getProjection();

 

// Draw point caption and its bounding rectangle

Paint p = new Paint();

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

p.setAntiAlias(true);

p.setTextSize(12);

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.setStyle(Style.FILL);

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

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

p.setStyle(Style.STROKE);

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

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

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

}

}

 

@Override

public int size() {

return(mOverlayItems.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);

mMyItemizedOverlay.addOverlay(mOverlayItem);

mMapView.getOverlays().add(mMyItemizedOverlay);

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel