2012-07-18 73 views
0

我想重寫覆蓋的Mapview的onTap函數,其中包含使用路徑繪製的一組線。我想知道是否有任何具有onTap線條的特定疊加層?添加onTap中的覆蓋圖中繪製的路徑

我的疊加看起來是這樣的:

public class MyPathOverlay extends Overlay { 
MapView map; 
Projection projection; 
ArrayList<Pair<GeoPoint, Integer>> pointsList; // Set of points and the 
               // color of the line 
               // starting from this point 

public MyPathOverlay(MapView contextMap, ArrayList<Pair<GeoPoint, Integer>> points) { 
    map = contextMap; 
    projection = map.getProjection(); 
    pointsList = points; 

} 

@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    super.draw(canvas, mapView, shadow); 
    if (shadow == false && pointsList !=null) { 

     for (int i = 0; i < pointsList.size() - 1; i++) { 
      Paint paint = new Paint(); 
      paint.setDither(true); 
      Pair<GeoPoint, Integer> p1 = pointsList.get(i); 
      GeoPoint gp1 = p1.first; 
      paint.setColor(p1.second); 
      GeoPoint gp2 = pointsList.get(i + 1).first; 
      paint.setStyle(Paint.Style.FILL_AND_STROKE); 
      paint.setStrokeJoin(Paint.Join.ROUND); 
      paint.setStrokeCap(Paint.Cap.ROUND); 
      paint.setStrokeWidth(5); 
      Point point1 = new Point(); 
      Point point2 = new Point(); 

      projection.toPixels(gp1, point1); 
      projection.toPixels(gp2, point2); 
      Path path = new Path(); 
      path.moveTo(point2.x, point2.y); 
      path.lineTo(point1.x, point1.y); 
      canvas.drawPath(path, paint); 
     } 
    } 
} 

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

    return super.onTap(p, mapView); 
} 

}

回答

2
@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); 
     Log.d("onTap","Path touched!!!"); 
    } 

    return super.onTap(geoPoint, mapView); 
} 
+0

非常感謝我的朋友。 – hashtpaa 2012-07-22 09:07:40

+0

這對我幫助非常大,謝謝 – eoghanm 2012-07-25 10:47:53

+0

爲什麼我在使用上面的代碼時將區域設置爲0,0,0,0?它不適用於我的項目。我在哪裏犯錯? – 2012-11-21 14:01:50