【发布时间】:2011-06-22 20:56:16
【问题描述】:
我正在尝试在 android 的地图视图上显示多边形。我创建了一个自定义覆盖类(多边形)并覆盖了 draw 方法。将多边形实例添加到地图视图的覆盖列表后,“应该”显示一个多边形。但是当显示地图时,找不到叠加层。我错过了什么?这是创建多边形的主要地图活动:
public class GPSLocator extends MapActivity {
MapView mapView;
Polygon polygon;
@Override
protected boolean isRouteDisplayed() {
return false;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
points.add(new GeoPoint((int)(-86.63283601665323*1e6), (int)(34.6857467079488*1e6)));
points.add(new GeoPoint((int)(-86.63172145427183*1e6), (int)(34.68572865382659*1e6)));
points.add(new GeoPoint((int)(-86.63172141228351*1e6), (int)(34.68613493108094*1e6)));
points.add(new GeoPoint((int)(-86.63288804303616*1e6), (int)(34.68611093719812*1e6)));
polygon = new Polygon(points);
mapView.getOverlays().clear();
mapView.getOverlays().add(polygon);
mapView.invalidate();
}
这是自定义叠加多边形:
public class Polygon extends Overlay {
ArrayList<GeoPoint> geoPoints;
public Polygon(ArrayList<GeoPoint> points)
{
geoPoints = points;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
//Set the color and style
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
//Create path and add points
Path path = new Path();
Point firstPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(0), firstPoint);
path.moveTo(firstPoint.x, firstPoint.y);
for(int i = 1; i < geoPoints.size(); ++i)
{
Point nextPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(i), nextPoint);
path.lineTo(nextPoint.x, nextPoint.y);
}
//Close polygon
path.lineTo(firstPoint.x, firstPoint.y);
path.setLastPoint(firstPoint.x, firstPoint.y);
canvas.drawPath(path, paint);
super.draw(canvas, mapView, shadow);
}
}
【问题讨论】:
标签: android overlay android-mapview polygon