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);
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);
Just override the OnTouch
returning true
:
mapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
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. :)
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.