GoogleMap 片段阻止自定义触摸事件
GoogleMap Fragment Blocks Custom Touch Events
我有一个 SupportMapFragment,我正在尝试绘制一条用户绘制的线,与 here 完全一样,我正在尝试使用替代方法 #2 使用此问题接受的答案,这似乎允许您将FrameLayout 中的 SupportMapFragment。但是,我无法让它为我工作。似乎 MapFragment 的视图已将 requestDisallowInterceptTouchEvent() 设置为 true,阻止我正确使用触摸事件,但我可以在不包含地图片段的屏幕部分使用触摸事件。我在这里看到的所有问题似乎都与我想做的相反。
这是我的 xml 与地图相关的代码(根线性布局包含其他视图组,与此无关):
<LinearLayout>
<FrameLayout
android:id="@+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/mapFrag" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
</LinearLayout>
MainActivity.onCreate() 中的 MapFragment 设置:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapFrag);
mapFragment.getMapAsync(this);
我尝试使用触摸事件的地方:
FrameLayout fram_map = (FrameLayout) findViewById(R.id.fram_map);
fram_map.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, @NonNull MotionEvent event){
float startX;
float startY;
float endX;
float endY;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
LatLng pt = mMap.getProjection().fromScreenLocation(new Point((int) startX,(int) startY));
System.out.println("Start: " + pt.toString());
break;
case MotionEvent.ACTION_MOVE:
//check for difference in prev once
endX = event.getX();
endY = event.getY();
System.out.println("Moving: " + endX);
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
endY = event.getY();
System.out.println("Stop: " + endX);
break;
}
return true;
}});
当我触摸地图区域(我禁用了 .setScrollGesturesEnabled())时,我在 logcat 中得到的所有内容:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
问题是您的地图片段在 FrameLayout
内。您需要更改布局:
<FrameLayout 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">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapFrag"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
android:id="@+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
此外,建议不要使用 System.out.println()
,而应使用 Log
。例如:Log.e("MOTION", "Start: " + pt.toString());
我有一个 SupportMapFragment,我正在尝试绘制一条用户绘制的线,与 here 完全一样,我正在尝试使用替代方法 #2 使用此问题接受的答案,这似乎允许您将FrameLayout 中的 SupportMapFragment。但是,我无法让它为我工作。似乎 MapFragment 的视图已将 requestDisallowInterceptTouchEvent() 设置为 true,阻止我正确使用触摸事件,但我可以在不包含地图片段的屏幕部分使用触摸事件。我在这里看到的所有问题似乎都与我想做的相反。
这是我的 xml 与地图相关的代码(根线性布局包含其他视图组,与此无关):
<LinearLayout>
<FrameLayout
android:id="@+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/mapFrag" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
</LinearLayout>
MainActivity.onCreate() 中的 MapFragment 设置:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapFrag);
mapFragment.getMapAsync(this);
我尝试使用触摸事件的地方:
FrameLayout fram_map = (FrameLayout) findViewById(R.id.fram_map);
fram_map.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, @NonNull MotionEvent event){
float startX;
float startY;
float endX;
float endY;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
LatLng pt = mMap.getProjection().fromScreenLocation(new Point((int) startX,(int) startY));
System.out.println("Start: " + pt.toString());
break;
case MotionEvent.ACTION_MOVE:
//check for difference in prev once
endX = event.getX();
endY = event.getY();
System.out.println("Moving: " + endX);
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
endY = event.getY();
System.out.println("Stop: " + endX);
break;
}
return true;
}});
当我触摸地图区域(我禁用了 .setScrollGesturesEnabled())时,我在 logcat 中得到的所有内容:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
问题是您的地图片段在 FrameLayout
内。您需要更改布局:
<FrameLayout 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">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapFrag"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
android:id="@+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
此外,建议不要使用 System.out.println()
,而应使用 Log
。例如:Log.e("MOTION", "Start: " + pt.toString());