-1

最近,我通过创建一个覆盖类并使用路径绘制它,在我的国家周围绘制了一个自定义多边形。我有很多地图扇区,每个扇区都被划分并用覆盖类填充颜色。但是,当我使用 ontap 函数时,只会调用最后一个覆盖项 ontap 函数。

我相信是因为我没有为叠加层设置边界?以下是我的叠加代码

public class SectorOverlay  extends  Overlay{

CustomPolygon customPolygon =null;
Context context;

public SectorOverlay(Context context, CustomPolygon customPolygon) {
    this.context=context;
    this.customPolygon =customPolygon;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) 
{
    shadow=false;

    Paint paint = new Paint();
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStrokeWidth(2);
    paint.setColor(0x10000000);     
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setAntiAlias(true);
    Point point1_draw = new Point();        

    if(customPolygon!=null)
    {

            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);



    }

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


    new CommonOpearation().showToast(context, customPolygon.getName());


return  true;
}
}
4

1 回答 1

2

它被称为“最后一个” Overlay,因为它是最顶层的。 MapView从 绘制它的叠加层0..end,但是当有一个事件时,最后一个绘制在顶部,所以它首先获取事件,并且因为你return trueOverlay.onTap(同样适用于Overlay.onTouchEvent,实际上很多 Android 事件)你说事件被处理,因此它不会打扰调用叠加层。所以事件处理程序是按end..0顺序调用的。

我没有使用onTap,但根据 Android 上的CommonsWare - Map overlay onTouchEvent / onTap howto?如果你使用ItemizedOverlay你应该onTap只为你的有界/绘制区域。这肯定是常见的onTap,并且onTouchEvents被要求用于屏幕上的任何触摸点。在这种情况下,您可以通过将其反向应用到 (x,y) 得到 (lon,lat)来找到GeoPointwith 。或者使用(同样,我以前没有听说过这个)。getProjectionMapViewonTap

如果您需要对覆盖的绘制多边形进行命中测试,这里有一个可以帮助您确定该点是否在(不一定是凸的)多边形中:http: //verkkopetus.cs.utu.fi/vhanke/ trakla/PointInPolygon.html

于 2012-07-26T22:03:00.117 回答