Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect drag on Android Google Maps

I'm developing an app using google maps API. I've got a boolean that tells me if I follow the user movement or not. I want to put it at false when the user drag the map. But how can I do that? That's my code

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/checkpoint"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"/>

and in the java class I call it

    mapFragment = new MapFragment();
    getFragmentManager().beginTransaction().add(R.id.map, mapFragment).commit();
    getFragmentManager().executePendingTransactions();
like image 341
Manuel Castro Avatar asked Dec 24 '22 19:12

Manuel Castro


1 Answers

You can create custom root layout which spies on touch events coming down to map fragment and use it instead of your default FrameLayout:

public class CustomFrameLayout extends FrameLayout {

    private GestureDetector gestureDetector;
    private IDragCallback dragListener;

    public CustomFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public interface IDragCallback {
        void onDrag();
    }

    public void setOnDragListener(IDragCallback listener) {
        this.dragListener = listener;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        gestureDetector.onTouchEvent(ev);
        return false;
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            //that's when user starts dragging
            if(dragListener != null) {
                dragListener.onDrag();
            }
            return false;
        }
    }
}

========= your_activity_layout.xml:

<com.your.package.CustomFrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/checkpoint"
    android:id="@+id/map"/>

========= YourActivity.java

CustomFrameLayout mapRoot = (CustomFrameLayout) findViewById(R.id.map);
mapRoot.setOnDragListener(this);

.......
@Override
public void onDrag() {
    //reset your flag here
}

Side note: You know that your android:name="com.google.android.gms.maps.SupportMapFragment" attribute in FrameLayout is useless.. right? You usually specify android:name attribute on <fragment> xml elements. Not layouts.

like image 110
Pavel Dudka Avatar answered Jan 02 '23 12:01

Pavel Dudka