February 10, 2012
Hot Topics:
RSS RSS feed

Mapping with Google APIs in Android

  • June 18, 2008
  • By Chunyen Liu
  • Send Email »
  • More Articles »

Figure 3: Traffic on satellite imagery

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
  • isShowMyLocation: Checks whether the current location is displayed on the map
  • isStreetView: Checks whether the map view is currently in street-view mode

Displaying Your Info on MapView

The base class Overlay 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 circle for the default position as well as enclosing its caption within a transparent rounded rectangle. Watch how I convert the position into screen coordinates with PixelCalculator, how I calculate the font metrics with Paint's measureText, and how I draw anti-aliased text with Paint's setAntiAlias.

// This is used draw an overlay on the map
protected class MyOverlay extends Overlay {
   @Override
   public void draw(Canvas canvas, PixelCalculator pc,
                    boolean shadow) {
      super.draw(canvas, pc, shadow);

      if (mDefCaption.length() == 0) {
         return;
      }

      Paint p = new Paint();
      int[] scoords = new int[2];
      int sz = 5;

      // Convert to screen coords
      pc.getPointXY(mDefPoint, scoords);

      // Draw point caption and its bounding rectangle
      p.setTextSize(14);
      p.setAntiAlias(true);
      int sw = (int)(p.measureText(mDefCaption) + 0.5f);
      int sh = 25;
      int sx = scoords[0] - sw / 2 - 5;
      int sy = scoords[1] - sh - sz - 2;
      RectF rec = new RectF(sx, sy, sx + sw + 10, sy + sh);

      p.setStyle(Style.FILL);
      p.setARGB(128, 255, 0, 0);
      canvas.drawRoundRect(rec, 5, 5, p);
      p.setStyle(Style.STROKE);
      p.setARGB(255, 255, 255, 255);
      canvas.drawRoundRect(rec, 5, 5, p);

      canvas.drawText(mDefCaption, sx + 5, sy + sh - 8, p);

      // Draw point body and outer ring
      p.setStyle(Style.FILL);
      p.setARGB(88, 255, 0, 0);
      p.setStrokeWidth(1);
      RectF spot = new RectF(scoords[0] - sz, scoords[1] + sz,
         scoords[0] + sz, scoords[1] - sz);
      canvas.drawOval(spot, p);

      p.setARGB(255, 255, 0, 0);
      p.setStyle(Style.STROKE);
      canvas.drawCircle(scoords[0], scoords[1], sz, p);
   }
}




Comment and Contribute

 


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

 

 


Networking Solutions
Sitemap | Contact Us