How to handle onTouch event for map in Google Map API v2?

后端 未结 2 1776
误落风尘
误落风尘 2020-11-27 06:17

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

I want to make custom

相关标签:
2条回答
  • 2020-11-27 06:50

    There is a simpler way to do this, handle your cases on onCameraMoveStarted listener like this

    Below the code snippet

    @Override
    public void onCameraMoveStarted(int reason) {
        if (reason == OnCameraMoveStartedListener.REASON_GESTURE) {
            Toast.makeText(this, "The user gestured on the map.",
                           Toast.LENGTH_SHORT).show();
        } else if (reason == OnCameraMoveStartedListener
                                .REASON_API_ANIMATION) {
            Toast.makeText(this, "The user tapped something on the map.",
                           Toast.LENGTH_SHORT).show();
        } else if (reason == OnCameraMoveStartedListener
                                .REASON_DEVELOPER_ANIMATION) {
            Toast.makeText(this, "The app moved the camera.",
                           Toast.LENGTH_SHORT).show();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:59

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

    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).

    Example code:

    UPDATE 1:

    • return original created view in getView()
    • use dispatchTouchEvent() instead of onInterceptTouchEvent()

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

    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);
            }
        }
    };
    
    0 讨论(0)
提交回复
热议问题