Android Google地图:禁用缩放滚动

时间:2017-04-03 14:42:30

标签: android scroll maps zoom pinch

我尝试为mapview创建自定义类但它不起作用。任何人都可以建议我一个方法。如何在用户尝试缩放时禁用滚动。不执行缩放时应启用滚动。

1 个答案:

答案 0 :(得分:0)

我使用下面的代码解决了这个问题。

在Class

中创建变量
private ScaleGestureDetector gestureDetector;
private long lastZoomTime = 0;
private float lastSpan = -1;
private long firstClick=0;
private long lastClick;
GoogleMap mMap;

在OnMapReady方法中添加以下代码:

mMap = googleMap;
gestureDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleGestureDetector.OnScaleGestureListener() {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            Log.i("onScale ", " onScale ");
            if (lastSpan == -1) {
                lastSpan = detector.getCurrentSpan();
            }
            if (detector.getEventTime() - lastZoomTime >= 50) {
                lastZoomTime = detector.getEventTime();
                mMap.animateCamera(CameraUpdateFactory.zoomBy(getZoomValue(detector.getCurrentSpan(), lastSpan)), 50, null);
                lastSpan = detector.getCurrentSpan();
            }
            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            lastSpan = -1;
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            lastSpan = -1;

        }


    });

在课堂上添加以下方法

private float getZoomValue(float currentSpan, float lastSpan) {
    Log.i("ongetZoomValue ", " getZoomValue ");
    double value = (Math.log(currentSpan / lastSpan) / Math.log(1.55d));
    return (float) value;
}

public boolean dispatchTouchEvent(MotionEvent ev) {

    //Log.i("on dispatchTouchEvent ", " dispatchTouchEvent ");
    switch (ev.getAction() & MotionEvent.ACTION_MASK) {




        case MotionEvent.ACTION_POINTER_DOWN:
            fingers = fingers + 1;

            break;
        case MotionEvent.ACTION_POINTER_UP:

            fingers = fingers - 1;
            break;
        case MotionEvent.ACTION_UP:
            fingers = 0;
            break;
        case MotionEvent.ACTION_DOWN:


            if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
                Log.i("on count=0", " count=0 ");
                count = 0;
            }
            count++;
            if (count == 1) {
                Log.i("on count ", " count=1 ");
                firstClick = System.currentTimeMillis();
            } else if (count == 2) {
                Log.i("on count ", " count=2 ");
                lastClick = System.currentTimeMillis();
                if (lastClick - firstClick < 300) {

                    CameraUpdate getzoom = CameraUpdateFactory.zoomIn();
                    mMap.animateCamera(getzoom, 400, null);
                    Log.i("on double click ", " event ");
                }

            }


            fingers=1;
            break;
    }

    if (fingers >1) {

        //Log.i("2fingersaction ", "2fingersaction ");
        mMap.getUiSettings().setScrollGesturesEnabled(false);
        //mMap.getUiSettings().setRotateGesturesEnabled(false);         // disableScrolling()
    }
    else if (fingers < 1 )
    {
        //Log.i("onefinger ", "onefinger ");

        mMap.getUiSettings().setScrollGesturesEnabled(true);
        // enableScrolling();
    }
    if (fingers > 1) {

        //Log.i("double if condition ", "doubleifcondition ");


        return gestureDetector.onTouchEvent(ev);

    } else

        return super.dispatchTouchEvent(ev);

}