Is there a Polygon class to be used in MapView?
Is there a third party lib or Android class like google.maps.Polygon
to be开发者_JAVA百科 used in MapView
(Native Google Maps APIs)? I have googled but I couldn't find it.
The is no class for drawing polygons in the Maps API. See documentation here:
http://code.google.com/android/add-ons/google-apis/reference/index.html
Below is some code I used to draw a route given a list of points. You could probably modify it to do what you need.
public class MapOverlay extends Overlay {
ArrayList<GeoPoint> route;
public MapOverlay(ArrayList<ParcelableGeoPoint> r) {
route = new ArrayList<GeoPoint>();
for (ParcelableGeoPoint p: r) {
route.add(p.getGeoPoint());
}
}
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.rgb(128, 136, 231));
mPaint.setAlpha(100);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
Path path = new Path();
GeoPoint start = route.get(0);
for (int i = 1; i < route.size(); ++i) {
Point p1 = new Point();
Point p2 = new Point();
Projection projection = mapv.getProjection();
projection.toPixels(start, p1);
projection.toPixels(route.get(i), p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x, p1.y);
start = route.get(i);
}
canvas.drawPath(path, mPaint);
}
}
精彩评论