Android - 在 MapView 上绘制路径作为叠加层

发布于 2024-09-05 17:49:51 字数 1131 浏览 5 评论 0原文

我有一个扩展 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);
}

如您所见,我在地图上列出了点列表,我希望它们形成一个多边形形状。

现在,问题是,当我将绘画样式设置为 FILL 或 FILL_AND_STROKE 时,屏幕上没有显示任何内容,但是当我将其设置为只是描边并设置描边宽度时,它实际上会绘制它应该绘制的内容。

现在,我寻找解决方案,但没有任何结果。你能告诉我我是否在代码本身中遗漏了一些设置,或者在覆盖画布上绘图时是否存在某种限制?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2024-09-12 17:49:51

有几件事。您应该仅使用 p.moveTo(from.x, from.y); 一次,即,当您第一次想要执行此操作时第一次使用。

尝试为用于绘制多边形的 paint 对象设置属性。

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

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文