2012-11-24 175 views
0

我已經完成了在我的覆蓋圖上繪製線條,但是如何繪製自由泳?不是像線條和圓圈那樣固定的幾何體,而是隻畫出我想要的地方!?android地圖覆蓋手繪

我已經是:

public class OverlayMap extends Overlay { 
    private List<MapGeoLine> geoLines = new ArrayList<MapGeoLine>(); 
    private GeoPoint geoFrom = null; 
    private GeoPoint geoTo = null; 

    @Override 
    public boolean onTouchEvent(MotionEvent motionEvent, MapView mapView) { 

     if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ 
      geoFrom = mapView.getProjection().fromPixels((int)motionEvent.getX(),(int)motionEvent.getY()); 
     } 

     if(motionEvent.getAction() == MotionEvent.ACTION_UP){ 
      geoTo = mapView.getProjection().fromPixels((int)motionEvent.getX(),(int)motionEvent.getY()); 
     } 

     if(geoFrom != null && geoTo != null){ 
      geoLines.add(new MapGeoLine(geoFrom, geoTo)); 
     }  

     return super.onTouchEvent(motionEvent, mapView); 
    } 

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

     if(geoLines.size() > 0){ 
      Paint mPaint = new Paint(); 
      mPaint.setStrokeWidth(2); 
      mPaint.setStyle(Paint.Style.STROKE); 
      mPaint.setAntiAlias(true); 

      Projection projection = mapView.getProjection(); 
      Path path = new Path(); 

      for(MapGeoLine line: geoLines){ 
       Log.d("test", "p1: "+line.getFrom().getLatitudeE6()+ " "); 
       Log.d("test", "p2: "+line.getTo().getLatitudeE6()+ " ");     

       Point from = new Point(); 
       Point to = new Point(); 

       projection.toPixels(line.getFrom(), from); 
       projection.toPixels(line.getTo(), to); 

       path.moveTo(from.x, from.y); 
       path.lineTo(to.x, to.y); 
      } 

      canvas.drawPath(path, mPaint); 
      mapView.invalidate(); 
     } 

     super.draw(canvas, mapView, shadow); 
    } 

} 
+0

嘿,你能幫我實施嗎?我有一些問題,應該在實施前明確。請 –

回答

4

您可以將代碼beloow做到這一點:

public class HandDrawOverlay extends Overlay { 

    private boolean editMode = false; 
    private boolean isTouched = false; 
    private Paint paint = new Paint(); 
    private Point screenPt1 = new Point(); 
    private Point screenPt2 = new Point(); 
    private ArrayList<GeoPoint> points = null; 

    public HandDrawOverlay(){ 
     paint.setStrokeWidth(2.0f); 
     paint.setStyle(Style.STROKE); 
     paint.setColor(Color.BLUE); 
    } 

    @Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     if(points != null && points.size() > 1){ 
      mapView.getProjection().toPixels(points.get(0), screenPt1); 
      for(int i=1; i<points.size();i++){ 
       mapView.getProjection().toPixels(points.get(i), screenPt2); 
       canvas.drawLine(screenPt1.x, screenPt1.y, screenPt2.x, screenPt2.y, paint); 
       screenPt1.set(screenPt2.x, screenPt2.y); 
      } 
     } 
    }  

    @Override 
    public boolean onTouchEvent(MotionEvent e, MapView mapView) { 
     if(editMode){ 
      int x = (int)e.getX(); 
      int y = (int)e.getY(); 
      GeoPoint geoP = mapView.getProjection().fromPixels(x,y); 

      switch (e.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       isTouched = true; 
       points = new ArrayList<GeoPoint>(); 
       points.add(geoP); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       if(isTouched) 
        points.add(geoP); 
       break; 
      case MotionEvent.ACTION_UP: 
       if(isTouched) 
        points.add(geoP); 
       isTouched = false; 
       break; 
      } 
      mapView.invalidate(); 
      return true; 
     } 
     return false; 
    } 

    /** 
    * @return the editMode 
    */ 
    public boolean isEditMode() { 
     return editMode; 
    } 

    /** 
    * @param editMode the editMode to set 
    */ 
    public void setEditMode(boolean editMode) { 
     this.editMode = editMode; 
    } 
} 

問候。

+0

Thx!我會嘗試!當你想畫什麼時,你是如何禁止地圖的移動的?它有點煩人,地圖隨着我的手指移動,當我想畫一條線...... – krackmoe

+1

只要你調用setEditMode(true)'onTouchEvent()'就開始返回true,通知'MotionEvent'已被消耗,因此不被「MapView」處理。 – Luis

+0

@Luis嘿,你能幫我實施嗎?我有一些問題,應該在實施前明確。請 –