问题描述
我试图在我的应用中实施Google地图,其中我想禁用谷歌地图中的两个手指滚动。这意味着如果用户用一个手指地图触摸地图应该滚动,并且如果用户在地图上触摸两个手指,它应该不滚动。
thanks!
您可以在GoogleMap上启用/停用滚动手势, setAllGesturesEnabled()和 setScrollGesturesEnabled() on GoogleMap.getUiSettings()对象并用于确定在 MapFragment 上双击手指,您可以使用基于自定义地图细分的方法
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);
休息;
case MotionEvent.ACTION_POINTER_DOWN:
//禁用滚动手势以防二手指向下
mGoogleMap.getUiSettings()。setScrollGesturesEnabled(false);
休息;
case MotionEvent.ACTION_POINTER_UP:
mGoogleMap.getUiSettings()。setScrollGesturesEnabled(true);
休息;
case MotionEvent.ACTION_UP:
mGoogleMap.getUiSettings()。setScrollGesturesEnabled(true);
休息;
}
返回super.dispatchTouchEvent(event);
code
$ b $ p $ MapFragment $ c
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);
返回mTouchView;
}
@Override
public View getView(){
return mOriginalContentView;
$ b $ p $ activity_main.xml code>对于 MainActivity 可以是:
< ?xml version =1.0encoding =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
工具: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扩展AppCompatActivity实现的OnMapReadyCallback {
私有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 (如果您使用它)。
I am trying to implement Google map in my app, in which I want to disable two finger scroll in google map.That means if user touches map with one finger map should be scrolled and if user touches two finger on map it should not scroll.thanks!
解决方案You can enable/disable scroll gestures at GoogleMap you can with calling setAllGesturesEnabled() and setScrollGesturesEnabled() on GoogleMap.getUiSettings() object and for determining touches of two fingers on MapFragment you can use approach with customized map fregment, based on this answer of community wiki:
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 for that should be customized like:
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 for MainActivity can be like:
<?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>and there is no changes in 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); } }Also, you can use same solution for customized MapView (if you use it).
这篇关于如何禁用谷歌地图中的两个手指滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!