android地图叠加手工绘制

时间:2012-11-24 10:56:23

标签: android overlay android-overlay

我已经完成了在我的叠加上画线,但我怎样才能画出自由泳!?没有固定的几何形状,如直线和圆形,但只是绘制在我想要的地方!?

我已经拥有的是:

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

}

1 个答案:

答案 0 :(得分:4)

您可以使用以下代码执行此操作:

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

问候。