Android - drawing path as overlay on MapView

2020-02-05 11:28发布

问题:

I have a class that extends Overlay and implemments Overlay.Snappable. I have overriden its draw method:

@Override
public void draw(Canvas canvas, MapView mv, boolean shadow)
{
    Projection projection = mv.getProjection();
    ArrayList<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
    //Creating geopoints - ommited for readability
    Path p = new Path();
    for (int i = 0; i < geoPoints.size(); i++) {
    if (i == geoPoints.size() - 1) {
        break;
    }
    Point from = new Point();
    Point to = new Point();
    projection.toPixels(geoPoints.get(i), from);
    projection.toPixels(geoPoints.get(i + 1), to);
    p.moveTo(from.x, from.y);
    p.lineTo(to.x, to.y);
    }
    Paint mPaint = new Paint();
    mPaint.setStyle(Style.FILL);
    mPaint.setColor(0xFFFF0000);
    mPaint.setAntiAlias(true);
    canvas.drawPath(p, mPaint);
    super.draw(canvas, mv, shadow);
}

As you can see, I make a list of points on a map and I want them to form a polygonal shape.

Now, the problem is that when I set paint style to be FILL or FILL_AND_STROKE nothing shows up on the screen, but when I set it to be just stroke, and set stroke width, it acctually draws what it is supposed to draw.

Now, I looked for solution, but nothing comes up. Can you tell me if I something missed to set in the code itself, or are there some sorts of constraints when drawing on Overlay canvases?

Thanks

回答1:

A few things. You should use p.moveTo(from.x, from.y); only once i.e., first time when you want to do it for the first time.

Try this to set attributes for the paint object used for painting the polygon.

polygonPaint = new Paint();
polygonPaint.setStrokeWidth(2); 
polygonPaint.setStyle(Paint.Style.STROKE);
polygonPaint.setAntiAlias(true); 

Hope this helps.