PhpLou PhpLou - 7 days ago 6
Android Question

Google Maps API gestures not working (zoom-rotate...)

Edit: I found my problem see my answer below

Original post:

I am creating an activity that contains a map, but I cannot add zooming and other gestures capability. I can double tap to zoom in, and I can add the +/- sign to zoom via

googleMap.getUiSettings().setAllGesturesEnabled(true);


This is my code:

Activity's xml (tracking_order.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<fragment
android:id="@+id/mapFragment"
class="com.example.OrderMapFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />

<FrameLayout
android:id="@+id/clientsFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />




OrderMapFragment.java

public class OrderMapFragment extends SupportMapFragment implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener {

public static OrderMapFragment newInstance() {
OrderMapFragment fragment = new OrderMapFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
((OrderMapFragment.OnOrderMarkerClickListener) context).setMapObject(this);
getMapAsync(this);
}

@Override
public void onMapReady(final GoogleMap googleMap) {
googleMap.getUiSettings().setZoomControlsEnabled(true); //this one is working
googleMap.getUiSettings().setZoomGesturesEnabled(true); //not working

googleMap.getUiSettings().setAllGesturesEnabled(true); //not working
// ... (removed code used to fetch markers data)
}

@Override
public boolean onMarkerClick(Marker marker) {
return listener.filterListByMarker(marker);
}

public interface OnOrderMarkerClickListener {
/**
* Action to be taken when a marker has been clicked
*
* @param marker
* @return true if the listener has consumed the event (i.e., the default behavior should not occur);
* false otherwise (i.e., the default behavior should occur).
* The default behavior is for the camera to move to the marker and an info window to appear.
*/
boolean filterListByMarker(Marker marker); //the class that implements this doesn't do anything for now it just returns false
}
}


Thank you

Answer

I finally found the problem:
The activity containing the mapFragment was extending a custom class. That class had overriden the following method:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_UP) {
        return super.dispatchTouchEvent(ev);
    }
    if (ev.getPointerCount() > 1)  //here was the problem
        return true;
    return super.dispatchTouchEvent(ev);
}

so I created a method and changed it to this:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_UP) {
        return super.dispatchTouchEvent(ev);
    }
    if (ev.getPointerCount() > 1 && !enableMultiTouch())
        return true;
    return super.dispatchTouchEvent(ev);
}

protected boolean enableMultiTouch(){
    return false;  //and override it to true in my activity
}

Hope it helps someone :)

Comments