java Android - 在 MapView 上绘制路径作为叠加层
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3036139/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - drawing path as overlay on MapView
提问by Rabas
I have a class that extends Overlay and implemments Overlay.Snappable. I have overriden its drawmethod:
我有一个扩展 Overlay 并实现 Overlay.Snappable 的类。我已经覆盖了它的draw方法:
@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.
现在,问题是当我将绘画样式设置为 FILL 或 FILL_AND_STROKE 时,屏幕上没有任何显示,但是当我将其设置为笔画并设置笔画宽度时,它实际上绘制了它应该绘制的内容。
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
谢谢
采纳答案by Soumya Simanta
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.
一些东西。您应该p.moveTo(from.x, from.y);只使用一次,即,当您第一次想这样做时第一次使用。
Try this to set attributes for the paintobject used for painting the polygon. 
试试这个来设置paint用于绘制多边形的对象的属性。
polygonPaint = new Paint();
polygonPaint.setStrokeWidth(2); 
polygonPaint.setStyle(Paint.Style.STROKE);
polygonPaint.setAntiAlias(true); 
Hope this helps.
希望这可以帮助。

