9

I'm trying to disable the zoom and pan the mapview in a mapbox. I use mapbox 0.4.0. Currently I can disable the zoom, but cannot disable the pan

    MapView mv = (MapView) tab.findViewById(R.id.mapid);
    mv.setZoom(14);
    mv.setMaxZoomLevel(14);
    mv.setMinZoomLevel(14);
stephen1706
  • 1,010
  • 11
  • 22

6 Answers6

24

In the current version (4.1), this should probably do:

map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(false);
map.getUiSettings().setScrollGesturesEnabled(false);

Or if you want to get rid of interaction completely:

map.getUiSettings().setAllGesturesEnabled(false);
Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
8

Just override the OnTouch returning true:

mapView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});
shaithana
  • 2,470
  • 1
  • 24
  • 37
  • 2
    Straightforward and lean. This should be the accepted answer. – Steffen Funke May 29 '16 at 08:46
  • 1
    Especially since Mapbox removes features from previous versions almost every release, this should be indeed be the answer. I was setting a lot of gesture properties but this line of code works the magic for me. Also don't make the same mistake as I did returning false at first. You return true to disable, false to enable! – frankhardenberg Jan 10 '23 at 10:50
4

I needed to do the same thing, But there is nothing relevant functionality I found in the MapBox sdk. So, I figure out something on my own. I customized MapView of MapBox sdk with enabling/disabling pan feature. For that I used SimpleTwoFingerDoubleTapDetector . Thanks @Sam for this above class which helps me to detect two finger double tap.

My custom MapView class:

MyMapView.java:

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;
import com.mapbox.mapboxsdk.tileprovider.MapTileLayerBase;
import com.mapbox.mapboxsdk.views.MapView;

public class MyMapView extends MapView implements OnGestureListener,OnDoubleTapListener {

    private GestureDetectorCompat mDetector;
    public boolean panShouldEnabled = true;

    public boolean isPanShouldEnabled() {
        return panShouldEnabled;
    }

    public void setPanShouldEnabled(boolean panShouldEnabled) {
        this.panShouldEnabled = panShouldEnabled;
    }

    public MyMapView(Context aContext, AttributeSet attrs) {
        super(aContext, attrs);
        init(aContext);
    }

    private void init(Context mContext) {
        mDetector = new GestureDetectorCompat(mContext, this);
        // Set the gesture detector as the double tap
        // listener.
        mDetector.setOnDoubleTapListener(this);
    }

    public MyMapView(Context aContext, int tileSizePixels,
            MapTileLayerBase tileProvider, Handler tileRequestCompleteHandler,
            AttributeSet attrs) {
        super(aContext, tileSizePixels, tileProvider,
                tileRequestCompleteHandler, attrs);
        init(aContext);
    }

    public MyMapView(Context aContext, int tileSizePixels,
            MapTileLayerBase aTileProvider) {
        super(aContext, tileSizePixels, aTileProvider);
        init(aContext);
    }

    public MyMapView(Context aContext) {
        super(aContext);
        init(aContext);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return !panShouldEnabled ? true : super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if (!panShouldEnabled) {
            if (detector.onTouchEvent(event)) {
                return true;
            } else {
                mDetector.onTouchEvent(event);
            }
        }
        return !panShouldEnabled ? true : super.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,float arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent event) {
        zoomIn();
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent event) {
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent arg0) {
        return false;
    }

    SimpleTwoFingerDoubleTapDetector detector = new SimpleTwoFingerDoubleTapDetector() {

        @Override
        public void onTwoFingerDoubleTap() {
            Toast.makeText(getContext(), "Two Finger Double Tap", Toast.LENGTH_SHORT).show();
            zoomOut();
        }
    };
}

So, now when you'll access your mapview in your Activity :

mapView = (MyMapView) findViewById(R.id.mapid);
//Here you can play with enabling/disabling pan. 
mapView.setPanShouldEnabled(false);

It might help someone. :)

Community
  • 1
  • 1
1

Unfortunately, the only solution is to put a transparent view on top of the map that will consume the touches. I use a Button with a transparent background. At this point MapBox still hasn't allowed devs to disable user interaction.

coder
  • 10,460
  • 17
  • 72
  • 125
  • Thinking of the same idea. I am struggling with an animation that is cancelled with a simple map touch. (disabling all gesture doesn't help me in my case, because it is just a touch behavior). – Tobliug Oct 23 '17 at 07:59
  • Just a sample link to give more context about a clickable="true" view : https://stackoverflow.com/questions/16377593/how-to-disable-behind-view-click-event-framelayout – Tobliug Oct 23 '17 at 08:43
1

Since at least 5.1.4 mapbox has the ability to disable all gestures at once

i.e. mapboxMap.uiSettings.setAllGesturesEnabled(false)

https://docs.mapbox.com/android/api/map-sdk/5.1.4/com/mapbox/mapboxsdk/maps/UiSettings.html#setAllGesturesEnabled-boolean-

William Reed
  • 1,717
  • 1
  • 17
  • 30
1

In mapbox v 10

mapView!!.gestures.quickZoomEnabled = false;
mapView!!.gestures.doubleTapToZoomInEnabled = false;
mapView!!.gestures.pinchToZoomEnabled = false;
shubomb
  • 672
  • 7
  • 20