如何处理onTouch事件在地图中的谷歌地图API第2版? [英] How to handle onTouch event for map in Google Map API v2?

查看:168
本文介绍了如何处理onTouch事件在地图中的谷歌地图API第2版?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GoogleMap的默认情况下不提供事件在地图拖动开始和拖动停止。 <一href="http://stackoverflow.com/questions/13702117/how-can-i-handle-map-move-end-using-google-maps-for-android-v2">I已经在这里报道这个问题。

GoogleMap by default doesn't provide event for map drag start and drag stop. I have already reported that problem here.

我想要自定义 处理将使用普通的 onTouch 活动,并结合它与 setOnCameraChangeListener

I want to make custom handler that will use plain onTouch event and combine it with setOnCameraChangeListener.

不过,我没有找到我怎么能访问 GoogleMap的对象 onTouch 事件。它不提供这样的回调

However i failed to find how I can access onTouch event of GoogleMap object. It doesn't provide such callback.

我不知道我该怎么处理onTouch事件在地图中的谷歌地图API V2

I wonder how can i handle onTouch event for map in Google Map API v2?

推荐答案

下面是一个可能的解决方法确定拖动开始和拖动结束事件:

Here is a possible workaround for determining drag start and drag end events:

您必须扩展 SupportMapFragment MapFragment 。在 onCreateView()不得不将你的图形页面在自定义的的FrameLayout (例如在它下面是 TouchableWrapper 的类),在其中截取的触摸事件,并确认地图是否被窃听或没有。如果你的 onCameraChange 被调用,只需查地图视图是pressed与否(例如下面这是变量 mMapIsTouched )。

You have to extend SupportMapFragment or MapFragment. In onCreateView() you have to wrap your MapView in a customized FrameLayout (in example below it is the class TouchableWrapper), in which you intercepts touch events and recognizes whether the map is tapped or not. If your onCameraChange gets called, just check whether the map view is pressed or not (in example below this is the variable mMapIsTouched).

例如code:

更新1:

  • getView回到原来创建的视图()
  • 使用 dispatchTouchEvent()而不是 onInterceptTouchEvent()
  • return original created view in getView()
  • use dispatchTouchEvent() instead of onInterceptTouchEvent()

自定义的FrameLayout:

Customized FrameLayout:

private class TouchableWrapper extends FrameLayout {
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
        mMapIsTouched = true;
        break;

    case MotionEvent.ACTION_UP:
        mMapIsTouched = false;
        break;
        }

        return super.dispatchTouchEvent(ev);
    }
    }

在自定义MapFragment:

In your customized MapFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);

    mTouchView = new TouchableWrapper(getActivity());
    mTouchView.addView(mOriginalContentView);

    return mTouchView;
}

@Override
public View getView() {
    return mOriginalContentView;
}

在你的相机变的回调方法:

In your camera change callback method:

private final OnCameraChangeListener mOnCameraChangeListener = new OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        if (!mMapIsTouched) {
            refreshClustering(false);
        }
    }
};

这篇关于如何处理onTouch事件在地图中的谷歌地图API第2版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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