androidoverlayandroid-overlay

android maps overlay draw by hand


I did already accomplish to draw lines on my overlay, but how can i draw freestyle!? Not fixed geometries like lines and circles but just draw wherever i want to!?

What i already have is:

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

}

Solution

  • You can you the code beloow to do it:

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

    Regards.