1

I have a SupportMapFragment and I am trying to draw a user drawn line, exactly like here, I am trying to use this questions accepted answer using Alternative Method #2, which seems to allow you to put the SupportMapFragment in a FrameLayout. However, I can't get it to work for me. It seems like the View of the MapFragment has requestDisallowInterceptTouchEvent() set to true, preventing me from consuming the touch events properly, but I can consume the touch events on the parts of the screen not containing the map fragment. All of the questions I've seen on here seem to do the opposite of what I want to do.

Here's my xml code relevant to the map (root linearlayout contains other viewgroups, not relevant to this):

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

MapFragment setup in MainActivity.onCreate():

SupportMapFragment mapFragment = (SupportMapFragment)    getSupportFragmentManager()
                .findFragmentById(R.id.mapFrag);

mapFragment.getMapAsync(this);

Where I'm trying to consume touch events:

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

All I get in my logcat when I touch in the map area (I disabled the .setScrollGesturesEnabled()) :

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
Community
  • 1
  • 1
M. Smith
  • 379
  • 1
  • 20

1 Answers1

2

The problem is that your map fragment is inside the FrameLayout. You need to change your layout:

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

Also, as a recommendation, don't use System.out.println(), use Log instead. For example: Log.e("MOTION", "Start: " + pt.toString());

antonio
  • 18,044
  • 4
  • 45
  • 61
  • Yes you're solution works, however it disables the ability to zoom in on the map. Do I have to then programmatically change the layout whenever I need to draw the map? – M. Smith Apr 05 '16 at 14:43
  • Yes, you need to programmatically change the behaviour of your view returning false on your `View.OnTouchListener`, or you can dispatch the `MotionEvent`s to the map using `map.dispatchTouchEvent (motionEvent);` – antonio Apr 05 '16 at 14:53