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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With