如何使用 Google Maps for Android V2 处理地图移动结束? [英] How can I handle map move end using Google Maps for Android V2?

查看:30
本文介绍了如何使用 Google Maps for Android V2 处理地图移动结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在更改地图中心后立即对地址进行地理编码.

I want to geocode address as soon as map center has been changed.

如何使用适用于 Android V2 的新 Google 地图处理地图移动?(我说的是用户用手指拖动地图的情况)

How can I handle map moveend with new Google Maps for Android V2? (I'm talking about the case then user drags map by finger)

推荐答案

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

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

您必须扩展 SupportMapFragment 或 MapFragment.在 onCreateView 中,您必须将 MapView 包装在自定义的 FrameLayout 中(在下面的示例中,它是TouchableWrapper"类),您可以在其中拦截触摸事件并识别地图是否被点击.如果您的onCameraChange"被调用,只需检查地图视图是否被按下(在下面的示例中,这是变量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").

示例代码:

更新 1:

  • 在 getView() 中返回原始创建的视图
  • 使用 dispatchTouchEvent() 而不是 onInterceptTouchEvent()

自定义框架布局:

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

这篇关于如何使用 Google Maps for Android V2 处理地图移动结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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