0

我目前有一些多边形形状,如下所示,它们正在使用以下代码绘制到我的地图视图上

在此处输入图像描述

    CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
            Path path = new Path();
            path.setFillType(Path.FillType.EVEN_ODD);
            for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
            {

                GeoPoint sector1 = new GeoPoint((int)(customPolygon.getCorrdinateList().get(n).getLatitude()*1e6), (int)((customPolygon.getCorrdinateList().get(n).getLongitude())*1e6));
                if(n==0){
                    mapView.getProjection().toPixels(sector1, point1_draw);
                    path.moveTo(point1_draw.x,point1_draw.y);
                }else
                {
                    mapView.getProjection().toPixels(sector1, point1_draw);
                    path.lineTo(point1_draw.x,point1_draw.y);
                }
            }

            path.close();
            canvas.drawPath(path, paint);

现在我正在考虑如何知道 ontap 按钮是否与任何多边形相交。例如,如果它与其中一个多边形相交,则会显示一条消息,显示当前多边形。

我被困在覆盖层的 ontap 部分。

    @Override
public boolean onTap(GeoPoint p, MapView ) {



        Point point1_draw = new Point();     
        mapView.getProjection().toPixels(p, point1_draw);


        for (int i =0;i<data.getCustomPolygonList().size();i++)
        {
            CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
            for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
            {
            }

        }

    return  true;
}
4

2 回答 2

1

我假设您需要一些代码来检查按钮是否在多边形内?

我现在不能给你代码,但这里有一个粗略的算法:

for each line segment in the polygon
  calculate the dot product for the line segment and the line formed by the starting vertex and the point to check
  if all dot products have the same sign, the point is inside the polygon

请注意,要使其正常工作,您需要多边形具有连续缠绕,即所有顶点都是顺时针或逆时针添加的。此外,这种方法可能并不总是适用于凹多边形。因此,您可能希望将凹多边形拆分为一系列凸多边形。

有关更通用(但也更昂贵)的算法,请查看此 wiki 页面:http ://en.wikipedia.org/wiki/Point_in_polygon

另一个信息来源(连同一些代码):如何确定 2D 点是否在多边形内?

于 2012-07-30T08:38:24.730 回答
0

我很惊讶 android.graphics.Path 和 android.graphics.PathMeasure 没有为此提供 API。它应该。

该算法在此处描述

于 2012-07-30T08:51:04.317 回答