android地图禁用运动

时间:2012-11-24 17:57:14

标签: android google-maps

我想用手指在谷歌地图上画一个叠加层。 我已经完成了画线。 但地图也用我的手指移动,所以我画得不太好,因为当地图也移动时,我只能在我的手指周围画一个小圆圈......

如何禁用地图的移动(拖动)并仍能用手指绘制和显示叠加层?

我目前的代码如下:

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

}

1 个答案:

答案 0 :(得分:0)

您需要在处理onTouchEvent()时从MotionEvent返回true,并且不希望其他任何人处理它。

否则您返回false,MapView将处理该事件,从而导致地图移动。

问候。