2013-01-15 150 views
0

好的,我已經使用谷歌,並沒有找到任何解決我的問題,我有一個MapView多個多邊形和一切正確顯示在地圖上。我試圖做的是添加一個onTap以顯示有關該多邊形的信息,但是我無法識別多邊形上的多邊形。以下是我目前擁有的。有人能告訴我在onTap部分我做錯了什麼嗎?Android多邊形覆蓋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); 
} 

} 

回答

0

你重用你path變量onDraw(),你有 -

path = new Path(); 

當你到onTap()只有最後一個路徑,你畫將被保存爲path一部分。所以你只會註冊最後一個多邊形的點擊。如果您稍後需要所有路徑,則應將其保存在List或其他集合中,並在onTap()中迭代它們。

或者,您可以將多邊形渲染爲單個的Path,並且只繪製並測試一次。你所遇到的問題是不知道哪個多邊形被點擊了,只是其中一個是。