Android googlemaps V2 - 禁用平移或缩放滚动

时间:2015-08-12 06:57:32

标签: android google-maps

我想在用户缩放时停用Android googlemaps上的滚动功能。在IOS谷歌地图中,​​它的那种简单,只需设置一个参数:self.settings.allowScrollGesturesDuringRotateOrZoom = NO; 不幸的是,我还没有在Android中找到这种功能,我正在寻找解决方案。我现在最好的想法是扩展googlemaps视图,截取视图上的触摸,如果触摸动作是捏,则手动设置缩放。现在,这在我看来相当骇人和不可靠。有一个更好的方法吗?还有其他建议或想法吗?我还没有任何关于此的代码,因此无法在此处包含任何代码。

对于某些情况,为什么需要这样做,是中心有一个用于标记地图精确中心的引脚,中心LatLng用于地址编码。

3 个答案:

答案 0 :(得分:5)

我最终做了一个完全自定义的解决方案,但由于ScaleGestureDetector错误,它只能用于API> 16.如果发布更新,我会找到解决方案。现在我使用常规Mapview,其缩放比例为API< = 16和API> 16我使用自定义类:

    public class CustomEventMapView extends MapView {

    private int fingers = 0;
    private GoogleMap googleMap;
    private long lastZoomTime = 0;
    private float lastSpan = -1;
    private Handler handler = new Handler();

    private ScaleGestureDetector gestureDetector;

    public CustomEventMapView(Context context, GoogleMapOptions options) {
        super(context, options);
    }

    public CustomEventMapView(Context context) {
        super(context);
    }

    @Override
    public void getMapAsync(final OnMapReadyCallback callback) {
        super.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final GoogleMap googleMap) {
                gestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {
                    @Override
                    public boolean onScale(ScaleGestureDetector detector) {
                        if (lastSpan == -1) {
                            lastSpan = detector.getCurrentSpan();
                        } else if (detector.getEventTime() - lastZoomTime >= 50) {
                            lastZoomTime = detector.getEventTime();
                            googleMap.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;

                    }
                });
                CustomEventMapView.this.googleMap = googleMap;
                callback.onMapReady(googleMap);
            }
        });
    }

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

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        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:
                fingers = 1;
                break;
        }
        if (fingers > 1) {
            disableScrolling();
        } else if (fingers < 1) {
            enableScrolling();
        }
        if (fingers > 1) {
            return gestureDetector.onTouchEvent(ev);
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

    private void enableScrolling() {
        if (googleMap != null && !googleMap.getUiSettings().isScrollGesturesEnabled()) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    googleMap.getUiSettings().setAllGesturesEnabled(true);
                }
            }, 50);
        }
    }

    private void disableScrolling() {
        handler.removeCallbacksAndMessages(null);
        if (googleMap != null && googleMap.getUiSettings().isScrollGesturesEnabled()) {
            googleMap.getUiSettings().setAllGesturesEnabled(false);
        }
    }
}

答案 1 :(得分:3)

这是一个老问题,但是我有同样的问题。经过一番谷歌搜索后,我找到了这个解决方案(我认为这是正确的解决方案): 让您的活动实现onMapReadyCallback,然后在您覆盖的onMapReady中设置googleMap.getUiSettings()。setScrollGesturesEnabledDuringRotateOrZoom(false); 那对我来说就是诀窍。

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener{
        GoogleMap gMap;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        gMap = googleMap;
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.getUiSettings().setZoomControlsEnabled(true);
        gMap.getUiSettings().setZoomGesturesEnabled(true);
        gMap.getUiSettings().setCompassEnabled(false);
        gMap.getUiSettings().setScrollGesturesEnabled(false);
        gMap.getUiSettings().setScrollGesturesEnabledDuringRotateOrZoom(false);
        gMap.setMyLocationEnabled(false);
        gMap.animateCamera(CameraUpdateFactory.zoomTo(8f));
    }

答案 2 :(得分:-2)

您需要做的就是将map:uiScrollGesturesmap:uiZoomGestures分配给false

map:uiScrollGestures="false"

map:uiZoomGestures="false"