简体   繁体   中英

Disable scrolling in Osmdroid map

I have an Osmdroid MapView. Even though I have set

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

the map can still be moved around. Is there a simple way to disable all interactions with the map view?

A simple solution is to do like @Schrieveslaach but with the mapView:

mapView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

You could try:

mapView.setEnabled(false); 

Which should disable all interactions with the map view

I've found a solution. You need to handle the touch events directly by setting a OnTouchListener . For example,

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;
    }
}

My solution is similar to @schrieveslaach and @sagix, but I just extend base MapView class and add new functionality:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM