【问题标题】:Canvas to draw polygon over mapview in Android画布在Android中的mapview上绘制多边形
【发布时间】:2011-11-09 00:04:09
【问题描述】:

我需要从几个点绘制一个多边形(我有它们的纬度,经度)。 我的实现基于这两个答案: 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);

我的问题是,我从哪里得到画布? 我的活动类中有所有这些代码。

谢谢!

【问题讨论】:

    标签: android canvas android-mapview android-maps


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,正如我在问题中引用的那样,我在我的解决方案中使用了这个问题。无论如何,现在我完全按照那里的答案进行了尝试(正如你所说的那样,它应该完成并且有效)我只是认为它可以通过其他方式完成。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多