【问题标题】:Disable scrolling in Osmdroid map在 Osmdroid 地图中禁用滚动
【发布时间】:2013-01-19 00:32:26
【问题描述】:

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

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

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

【问题讨论】:

  • 您找到解决方案了吗?

标签: java android maps osmdroid openstreetmap


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我找到了解决方案。您需要通过设置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】:

      你可以试试:

      mapView.setEnabled(false); 
      

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

      【讨论】:

      • 对我不起作用
      【解决方案4】:

      我的解决方案类似于@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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多