2

I want to draw with my finger an overlay on google maps. I already accomplished that the line is drawn.. But the map moves also with my finger and so i cant draw very well, because i just can draw a small circle around my finger when the map also moves...

How can i disable the movement (dragging) of the map and be still able to draw and display an overlay with my finger?

My current Code looks like this:

public class OverlayMap extends Overlay {
    private List<MapGeoLine> geoLines = new ArrayList<MapGeoLine>();
    private List<GeoPoint> geoPoints;
    private boolean isTouched = false;

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

        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ 
            geoPoints = new ArrayList<GeoPoint>();
            geoPoints.add(mapView.getProjection().fromPixels((int)motionEvent.getX(),(int)motionEvent.getY()));
            isTouched = true;
        }

        if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
            if(isTouched){
                geoPoints.add(mapView.getProjection().fromPixels((int)motionEvent.getX(),(int)motionEvent.getY()));
            }
        }

        if(motionEvent.getAction() == MotionEvent.ACTION_UP){
            if(isTouched){
                geoPoints.add(mapView.getProjection().fromPixels((int)motionEvent.getX(),(int)motionEvent.getY()));
            }           
            isTouched = false;
        }   

        return super.onTouchEvent(motionEvent, mapView);
    }

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

        if(geoPoints != null && geoPoints.size() > 1 && !isTouched){
            Paint mPaint = new Paint();
            mPaint.setStrokeWidth(2); 
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setAntiAlias(true); 

            Projection projection = mapView.getProjection();

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

            projection.toPixels(geoPoints.get(0), from);            

            for(int i = 1; i < geoPoints.size(); i++){          
                projection.toPixels(geoPoints.get(i), to);
                p.moveTo(from.x, from.y);
                p.lineTo(to.x, to.y);
                from.set(to.x, to.y);               
            }  

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

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

}
Marcelo
  • 9,387
  • 3
  • 35
  • 40
krackmoe
  • 1,743
  • 7
  • 29
  • 53

1 Answers1

0

You need to return true from onTouchEvent() when you process the MotionEvent and don't want it to be processed by anyone else.

Otherwise you return false, and the event will be processed by the MapView resulting in a map movement.

Regards.

Luis
  • 11,978
  • 3
  • 27
  • 35
  • Its not working in my case can you please help me. [see my question & answer](http://stackoverflow.com/questions/20901141/how-to-draw-free-hand-polygon-in-google-map-v2-in-android/20916931#20916931) – Chintan Khetiya Jan 04 '14 at 08:48