1

我正在编写一个需要在地图上显示多边形的应用程序。我研究并发现最好的方法是继承 Overlay 类并覆盖 draw 方法。它工作得很好——但是当我滚动或平移时,原始多边形的形状和画布位置不会消失——所以当我平移时,我会不断得到越来越多相同形状的多边形——缩放时也是如此。这是我的自定义叠加层的代码。一旦我得到多边形形状的坐标,我就会调用 setOverlayType 方法——这样 draw 方法就有了它需要的数据来确定在哪里绘制多边形。

这可能是愚蠢的事情!

public class myOverLay extends Overlay {

public final int OVERLAY_TYPE_NORMAL = 0;
public final int OVERLAY_TYPE_SCALED = 1;

private GeoPoint pVerts[];
private GeoPoint pCenter;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Path path = new Path();
private Context mContext;
private int overlayType;
private String pName = "";

public myOverLay(Context context) {
    super();
    this.mContext=context;
}

public void setOverlayType(GeoPoint[] polyVerts, GeoPoint polyCenter, int overlayT, String pn) {
    this.pVerts = polyVerts;
    this.pCenter = polyCenter;
    this.overlayType = overlayT;
    this.pName = pn;
}   
@Override
public void draw(Canvas canvas, MapView mapview, boolean shadow)
{
super.draw(canvas,mapview,false);
Projection projection = mapview.getProjection();
Point pVert = new Point(0,0);

if(this.overlayType==0)
    paint.setColor(android.graphics.Color.argb(50, 255, 0, 0)); 
else
    paint.setColor(android.graphics.Color.argb(50, 255, 255, 0));

paint.setStrokeWidth(2);    
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);

path.setFillType(Path.FillType.EVEN_ODD);
projection.toPixels(this.pVerts[0],pVert);
path.moveTo(pVert.x, pVert.y);
for(int x=0;x<=5;x++)
{
    projection.toPixels(this.pVerts[x],pVert);
    path.lineTo(pVert.x, pVert.y);
}
path.close();

Log.v("OVERLAY", "Draw Method Called");
canvas.drawPath(path, paint);

if(this.overlayType==0) //Icon
{   
    Point centerPt = new Point();
    projection.toPixels(this.pCenter, centerPt);
    Bitmap bmap=BitmapFactory.decodeResource(this.mContext.getResources(),     R.drawable.my_image);
    bmap = Bitmap.createScaledBitmap(bmap,20, 40, true);
    canvas.drawBitmap(bmap, centerPt.x-10, centerPt.y-50, null);
}
}

}
4

1 回答 1

0

回答我自己的问题 - 我将 Paint 和 Path 变量从类变量移动到 draw 方法中的局部变量 - 确实很愚蠢 - 地图现在可以很好地平移、缩放和滚动多边形。

于 2012-10-31T03:30:59.753 回答