Android的GOOGLEMAPS V2 - 上平移或缩放滚动禁用 [英] Android googlemaps V2 - Disable scroll on pan or zoom

查看:352
本文介绍了Android的GOOGLEMAPS V2 - 上平移或缩放滚动禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android GOOGLEMAPS禁用滚动当用户缩放。在IOS谷歌地图,它的那种轻松,只需设置一个参数:self.settings.allowScrollGesturesDuringRotateOrZoom = NO;
不幸的是,我还没有发现这样的功能在Android和进出口寻找解决方案。我现在最好的办法是延长GOOGLEMAPS观点,拦截视图上的联系,如果触摸操作是捏然后手动设置缩放。现在,这对我来说非常哈克和不可靠的。有没有更好的方式来做到这一点?其他任何建议或想法?我没有任何code没有关于这一点,所以着包括任何在这里。

I would like to disable scroll on Android googlemaps when the user is zooming. In IOS google maps, its kind of easy, just set a parameter: self.settings.allowScrollGesturesDuringRotateOrZoom = NO; Unfortunately, I have not found this kind of feature in Android and Im looking for solutions. The best idea I have right now is to extend the googlemaps view, intercept the touch on the view and if the touch action is pinch then set the zoom manually. Now this seems to me quite hacky and unreliable. Is there a better way to do this? Any other suggestions or ideas? I do not have any code yet about that, so cant include any of it here.

有关某些上下文为什么这需要做,是有在其中用于标记在地图上并经纬度用于地理编码的地址的中心的正中心的中心的销。

For some context why this needs to be done, is that there is a pin in the centre which is used to mark the exact center of the map and the center LatLng is used for geocoding addresses.

推荐答案

最后我做一个完全自定义的解决方案,但因为ScaleGestureDetector的bug,将工作仅API> 16.将发布更新,如果我会找到一个解决方案那。现在我用endbled的API&LT规模手势正规的MapView = 16和API> 16我用我的自定义类:

I ended up doing a completely custom solution, although because of ScaleGestureDetector bug it will work only API > 16. Will post updates if this I will find a solution to that. Right now I use regular Mapview with scale gestures endbled on API <= 16 and API > 16 I use my custom class:

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

这篇关于Android的GOOGLEMAPS V2 - 上平移或缩放滚动禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆