2

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

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

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

4

4 回答 4

5

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

mapView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});
于 2016-03-14T17:30:58.360 回答
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;
    }
}
于 2016-02-04T08:28:33.827 回答
0

你可以试试:

mapView.setEnabled(false); 

这应该禁用与地图视图的所有交互

于 2013-01-19T02:44:12.573 回答
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
    }
}
于 2020-03-23T06:27:25.700 回答