GoogleMap片段阻止自定义触摸事件

时间:2016-04-01 16:39:31

标签: android google-maps android-fragments google-maps-android-api-2

我有一个SupportMapFragment,我正在尝试绘制一个用户绘制的行,与here完全一样,我正在尝试使用替代方法#2使用此问题接受的答案,这似乎允许您放置SupportMapFragment在FrameLayout中。但是,我无法让它为我工作。看起来MapFragment的View将requestDisallowInterceptTouchEvent()设置为true,阻止我正确地使用触摸事件,但我可以在不包含地图片段的屏幕部分上使用触摸事件。我在这里看到的所有问题似乎都与我想要做的相反。

这是我与地图相关的xml代码(root linearlayout包含其他视图组,与此无关):

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

当我触摸地图区域时,我进入了我的logcat(我禁用了.setScrollGesturesEnabled()):

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

1 个答案:

答案 0 :(得分:1)

问题是您的地图片段位于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());

相关问题