我想在android中的mapview上显示一个多边形。我创建了一个自定义覆盖类(多边形)并覆盖了draw方法。将多边形实例添加到mapview的叠加列表后,应显示多边形“应该”。但是当显示地图时,没有找到叠加层。我错过了什么?以下是创建多边形的主要地图活动:
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);
}
}
答案 0 :(得分:0)
你必须使用path.moveTo(pixelPoint.x,pixelPoint.y);在你的绘图循环中。否则,您将绘制一个不是多边形的星。这是我的(工作)代码,用于执行类似的任务:
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
Log.i("BikeComputer", "MyOverlay draw");
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);
Point p1 = new Point();
path = new Path();
GeoPoint point = geoPoints.get(0);
mapv.getProjection().toPixels(point, p1);
path.moveTo(p1.x, p1.y);
Point pixelPoint = new Point();
Iterator<GeoPoint> i = geoPoints.iterator();
while (i.hasNext()) {
GeoPoint trackPoint = i.next();
mapv.getProjection().toPixels(trackPoint, pixelPoint);
path.lineTo(pixelPoint.x, pixelPoint.y);
path.moveTo(pixelPoint.x, pixelPoint.y);
}
// draw track
canvas.drawPath(path, mPaint);
// start point
mapv.getProjection().toPixels(geoPoints.get(0), pixelPoint);
canvas.drawBitmap(startFlag, pixelPoint.x, pixelPoint.y - 32, null);
// finish point
mapv.getProjection().toPixels(geoPoints.get(geoPoints.size() - 1),
pixelPoint);
canvas.drawBitmap(finishFlag, pixelPoint.x, pixelPoint.y - 32, null);
}
}
这个课程如何实现:
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
mapView.getProjection();
mapOverlays.add(new MyOverlay());
答案 1 :(得分:0)
正如@piotrpo在评论中所说:
你真的想在南极附近绘制多边形吗?
你的纬度是-86.63283601665323;我在文档中读到了这个:
这将被夹在-80度到+80度之间, 为了保持墨卡托投影的准确性。
所以你应该避免靠近极点的坐标。
但我不知道这是否是你的多边形没有出现的原因,因为它可能应该自动“修复”坐标而不是让它变得格格不入。