PhpLou
PhpLou

Reputation: 440

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

Upvotes: 1

Views: 2007

Answers (3)

Micer
Micer

Reputation: 8979

In my case it helped to set map.getUiSettings().setAllGesturesEnabled(true); and to override lifecycle methods as described in MapView documentation:

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mapView.onCreate(bundle);
    }

    @Override
    public void onSaveInstanceState()
    {
        super.onSaveInstanceState();
        mapView.onSaveInstanceState();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStart()
    {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onLowMemory()
    {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onStop()
    {
        super.onStop();
        mapView.onStop();
    }

Upvotes: 0

PhpLou
PhpLou

Reputation: 440

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

Upvotes: 2

Make it Simple
Make it Simple

Reputation: 1882

Comment this line

googleMap.getUiSettings().setZoomGesturesEnabled(true); //not working

Now you try, it will work. If you need any particular or few Gestures means, enable by separate otherwise if you need all just call this line

googleMap.getUiSettings().setAllGesturesEnabled(true);

Upvotes: 2

Related Questions