如何在谷歌地图中禁用两个手指滚动

时间:2018-02-16 10:14:21

标签: android google-maps

我正在尝试在我的应用中实施Google地图,其中我要禁用谷歌地图中的两个手指滚动。这意味着如果用户触摸地图,一个手指地图应滚动,如果用户触摸两个手指在地图上它应该不滚动。 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以在setAllGesturesEnabled()对象上调用setScrollGesturesEnabled()GoogleMap.getUiSettings()来启用/禁用Google地图上的滚动手势,并确定您可以使用的MapFragment上两根手指的触摸根据社区维基的this答案,使用自定义地图频段进行处理:

public class TouchableWrapper extends FrameLayout {

    private GoogleMap mGoogleMap = null;

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

    public void setGoogleMap(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
            break;

            case MotionEvent.ACTION_POINTER_DOWN:
                // disabling scroll gestures in case second finger down
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(false);
            break;

            case MotionEvent.ACTION_POINTER_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
            break;

            case MotionEvent.ACTION_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
            break;
        }

        return super.dispatchTouchEvent(event);
    }
}

MapFragment应该像以下一样定制:

public class MultiTouchMapFragment extends MapFragment {
    public View mOriginalContentView;
    public TouchableWrapper mTouchView;

    @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;
    }
}
activity_main.xml的{​​p> MainActivity可以是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="{YOUR_PACKAGE_NAME}.MainActivity">

    <fragment
        android:id="@+id/map_fragment"
        android:name="{YOUR_PACKAGE_NAME}.MultiTouchMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

MainActivity

没有变化
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;
    private MultiTouchMapFragment mMapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMapFragment = (MultiTouchMapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mMapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mMapFragment.mTouchView.setGoogleMap(mGoogleMap);
    }

}

此外,您可以对自定义MapView使用相同的解决方案(如果您使用它)。

答案 1 :(得分:0)

试试这个:

<item name="android:splitMotionEvents">false</item>
    <item name="android:windowEnableSplitTouch">false</item>

将其放入您的活动主题