Mapping with Google APIs in Android

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);
}
}
0 Comments (click to add your comment)
Networking Solutions



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.
Discover how to start developing for the Android platform with this extensive guide, which provides a reference to the Android platform as well as a look at developing your first Android application. You'll explore the top 10 features for developers as well as learn design and development tips that go beyond the phone and target tablet development as well.