在Osmdroid地图中禁用滚动

时间:2013-01-19 00:32:27

标签: java android maps osmdroid openstreetmap

我有一个Osmdroid MapView。即使我已经设置

mapView.setClickable(false);
mapView.setFocusable(false);

地图仍然可以移动。是否有一种简单的方法可以禁用与地图视图的所有交互?

4 个答案:

答案 0 :(得分:2)

一个简单的解决方案是像@Schrieveslaach那样使用mapView:

background-image

答案 1 :(得分:1)

你可以尝试:

mapView.setEnabled(false); 

应禁用与地图视图的所有互动

答案 2 :(得分:1)

我找到了解决方案。您需要通过设置OnTouchListener直接处理触摸事件。例如,

public class MapViewLayout extends RelativeLayout {

    private MapView mapView;

    /**
     * @see #setDetachedMode(boolean)
     */
    private boolean detachedMode;

    // implement initialization of your layout...

    private void setUpMapView() {
       mapView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (detachedMode) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        // if you want to fire another event
                    }

                    // Is detached mode is active all other touch handler
                    // should not be invoked, so just return true
                    return true;
                }

                return false;
            }
        });
    }

    /**
     * Sets the detached mode. In detached mode no interactions will be passed to the map, the map
     * will be static (no movement, no zooming, etc).
     *
     * @param detachedMode
     */
    public void setDetachedMode(boolean detachedMode) {
        this.detachedMode = detachedMode;
    }
}

答案 3 :(得分:0)

我的解决方案类似于@schrieveslaach和@sagix,但我只是扩展了基础MapView类并添加了新功能:

class DisabledMapView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : MapView(context, attrs) {

    private var isUserInteractionEnabled = true

    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        if (isUserInteractionEnabled.not()) {
            return false
        }
        return super.dispatchTouchEvent(event)
    }

    fun setUserInteractionEnabled(isUserInteractionEnabled: Boolean) {
        this.isUserInteractionEnabled = isUserInteractionEnabled
    }
}