Android多边形叠加ontap

时间:2013-01-15 01:57:54

标签: java android android-mapview

好的,我已经使用了谷歌并没有找到任何解决我问题的方法,我在MapView上有多个多边形,所有内容都正确显示在地图上。我想要做的是添加一个onTap来显示有关多边形的信息,但我无法识别多边形上的点击。以下是我目前的情况。有人可以告诉我onTap部分我做错了什么吗?

public class Polygon extends Overlay {
ArrayList<GeoPoint> geoPoints;
static ArrayList<String> custCount;
static ArrayList<String> hexCode;
static ArrayList<String> custMin;
static ArrayList<String> custMax;
static String tester;
Point firstPoint;
static List<HashMap<String, String>> colorRanges;

private Path path;

public Polygon(ArrayList<GeoPoint> points){
    geoPoints = points;
}
public class setColorRanges{



public setColorRanges(List<HashMap<String, String>> colorData) {

     colorRanges = colorData;
  }

}

public class  CustCount{
    public CustCount(ArrayList<String> str) {
        custCount = str;
    }
 }

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow){


    //Set the color and style
    Paint paint = new Paint();
    Paint paint1 = new Paint();

    //Create path and add points

    int origin = 0;
    for(int i = 0; i < geoPoints.size()/5;i++){
        path = new Path();
        firstPoint = new Point();

        mapView.getProjection().toPixels(geoPoints.get(origin), firstPoint);
        path.moveTo(firstPoint.x, firstPoint.y);

        for(int j=0; j<5; j++) {
            Point nextPoint = new Point();
            mapView.getProjection().toPixels(geoPoints.get(origin+j), nextPoint);
            path.lineTo(nextPoint.x, nextPoint.y);

        }
        //loop thru array of color ranges
        int curCustCount = Integer.valueOf(custCount.get(i));
        String curColor = "";
        for(int z=0; z<colorRanges.size(); z++) {
            int custmin = Integer.valueOf( colorRanges.get(z).get("custmin") );
            int custmax = Integer.valueOf( colorRanges.get(z).get("custmax") );

            if( curCustCount >= custmin && curCustCount <= custmax) {
                curColor = colorRanges.get(z).get("hexcode");
                break;
            }
        }

        paint.setColor(Color.parseColor(curColor));
        paint.setAlpha(70);
        paint.setStyle(Paint.Style.FILL);
        paint1.setColor(Color.parseColor(curColor));
        paint1.setAlpha(100);
        paint1.setStyle(Paint.Style.STROKE);

        path.lineTo(firstPoint.x, firstPoint.y);
        path.setLastPoint(firstPoint.x, firstPoint.y);
        canvas.drawPath(path, paint);
        canvas.drawPath(path,paint1);

        origin += 5;
    }

    super.draw(canvas, mapView, shadow);
}
@Override
public boolean onTap(GeoPoint geoPoint, MapView mapView) {

    RectF rectF = new RectF();
    path.computeBounds(rectF, true);
    Region region = new Region();
    region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));

    Point point = new Point();
    mapView.getProjection().toPixels(geoPoint, point);

    if (region.contains(point.x, point.y)) {
        Log.d("onTap", point.x+" "+point.y);
    }

    return super.onTap(geoPoint, mapView);
}

}

1 个答案:

答案 0 :(得分:0)

您正在path重新使用onDraw()变量,您有 -

path = new Path();

当您到达onTap()时,只会将您绘制的最后一条路径保存为path的一部分。因此,您只会在最后一个多边形上注册点击。如果您稍后需要所有路径,则应将它们保存在List或其他集合中,并在onTap()中迭代它们。

或者,您可以将多边形渲染为单个Path,并仅绘制并测试一次。那里的问题是你不知道点击了哪个多边形,只是其中一个是。