android - Canvas 在 Android 的 mapview 上绘制多边形

标签 android canvas android-mapview android-maps

我需要从几个点(我有它们的纬度、经度)绘制一个多边形。 我的实现基于这两个答案: Drawing an empty polygon given a set of points on a Map Overylay (Android 2.1) Drawing a line/path on Google Maps

在我的 MapOverlayAction.java 中,我为一些引脚设置了叠加层,如下所示:

mapOverlays.add(itemizedoverlay);
setLocationOverlay(mapView, mapController);

其中 itemizedoverlay 是一个 OverlayItems 数组

这很好用。但我还需要为这些点绘制一个多边形(其中每个点都是一个顶点)。所以我做的是:

Path path = new Path();

 for (int j = 0; j < itemizedoverlay.size(); j++) {

   GeoPoint gP1 = itemizedoverlay.getItem(j).getPoint();
   Point currentScreenPoint = new Point();

    Projection projection = mapView.getProjection();
    projection.toPixels(gP1, currentScreenPoint);

    if (j == 0)
      path.moveTo(currentScreenPoint.x, currentScreenPoint.y); 
    else
      path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
}

在我基于我的解决方案的两个答案中,调用了以下方法:

    Paint   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(2);    

    canvas.drawPath(path, mPaint);

我的问题是,我从哪里得到 Canvas ? 我的 Activity 课中有所有这些代码。

谢谢!

最佳答案

您需要继承 Overlay 类并覆盖 Draw获取 Canvas 的方法。

然后实例化您的新类并将其添加到叠加层列表以使其显示在 map 上。这question应该有帮助。

关于android - Canvas 在 Android 的 mapview 上绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8058788/

相关文章:

java - 当我尝试运行新的示例应用程序时,无法在Flutter中打开xmltree错误

Javascript HTML5 捕获 keyCode 并写入 Canvas

新 map 应用程序中的 iPhone map 折叠按钮

iphone - 如何缩放覆盖 View 下方的 MapView 注释?

android - 确保在方向更改后保留视频缓冲区,而无需手动处理 onConfiguration 更改

java - 保存多选 ListView 的状态

android - 尝试减少应用程序内存时,我应该记录哪些值?

javascript - HTML5/JS - 一个不依赖 Canvas 的优秀游戏引擎?

javascript - 在 JavaScript 中计算 Canvas 数组中位数的有效方法

android - 通过 Zoom、Android 缩放 map 标记