我正在尝试禁用缩放并在地图框中平移地图视图。我使用mapbox 0.4.0。目前我可以禁用缩放,但无法禁用平移
MapView mv = (MapView) tab.findViewById(R.id.mapid);
mv.setZoom(14);
mv.setMaxZoomLevel(14);
mv.setMinZoomLevel(14);
答案 0 :(得分:17)
在当前版本(4.1)中,这可能应该这样做:
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(false);
map.getUiSettings().setScrollGesturesEnabled(false);
或者如果你想彻底摆脱互动:
map.getUiSettings().setAllGesturesEnabled(false);
答案 1 :(得分:6)
只需覆盖OnTouch
返回true
:
mapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
答案 2 :(得分:4)
我需要做同样的事情,但是我在MapBox sdk中找不到任何相关的功能。所以,我自己找出了一些东西。我通过启用/禁用平移功能自定义了MapBox sdk的MapView。为此,我使用了SimpleTwoFingerDoubleTapDetector。感谢@Sam上面的课程,这有助于我检测两个手指双击。
我的自定义MapView类:
<强> 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();
}
};
}
所以,现在当你在Activity中访问你的mapview时:
mapView = (MyMapView) findViewById(R.id.mapid);
//Here you can play with enabling/disabling pan.
mapView.setPanShouldEnabled(false);
它可能对某人有所帮助。 :)
答案 3 :(得分:1)
不幸的是,唯一的解决方案是在地图上放置一个透明视图,消耗触摸。我使用透明背景的Button
。此时,MapBox 仍然不允许开发人员禁用用户交互。
答案 4 :(得分:0)
由于至少5.1.4映射框具有一次禁用所有手势的功能
即mapboxMap.uiSettings.setAllGesturesEnabled(false)