【问题标题】:Placing Zoom Controls in a MapView在 MapView 中放置缩放控件
【发布时间】:2010-09-20 18:44:54
【问题描述】:

我正在尝试让缩放控件显示在 mapview 中,以下代码几乎可以工作,但缩放控件出现在 mapview 的左上角,而不是像我这样的底部中心通过setGravity() 指定。有人可以告诉我我缺少什么吗?

zoomView = (LinearLayout) mapView.getZoomControls();
zoomView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); 
zoomView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
mapView.addView(zoomView);

这些视图/布局都是以编程方式构建的,没有需要调整的布局文件。

【问题讨论】:

    标签: android android-mapview


    【解决方案1】:

    将以下行添加到您的MapView 类的OnCreate() 方法中:

    view.setBuiltInZoomControls(true);

    【讨论】:

      【解决方案2】:

      这里的诀窍是在您要放置 ZoomControls 的位置放置另一个 Layout 容器,然后将 ZoomControls 插入其中。

      真正的技巧是使用RelativeLayout 而不是LinearLayout 来定位元素,如此示例中所示layout.xml

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"> 
        <com.google.android.maps.MapView
          android:id="@+id/myMapView"
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:enabled="true"
          android:clickable="true"
          android:apiKey="MY_MAP_API_KEY"
        />
        <LinearLayout android:id="@+id/layout_zoom" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentBottom="true" 
          android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 
      

      layout_zoom LinearLayout 元素位于屏幕底部中心,将其置于MapView 的中间/底部。

      然后在您的 Activity 的 onCreate 中,获取对 layout_zoom 元素的引用并将 ZoomControl 插入其中,就像您已经完成的那样:

      LinearLayout zoomLayout =(LinearLayout)findViewById(R.id.layout_zoom);  
      View zoomView = myMapView.getZoomControls(); 
      zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      myMapView.displayZoomControls(true);
      

      ZoomControls 现在应该会在长按时出现,而不会窃取地图触摸事件。

      【讨论】:

      • 你好,我可以编辑你的答案,在 WebView 上添加一个实现吗?就几个4-5行。我使用谷歌并找到了这个解决方案。但是,要在 WebView 上使用它,我需要稍作修改。
      【解决方案3】:

      上面的方法对我不起作用,但是这样做(将控件放在右下角):

      mapView.setBuiltInZoomControls(true);
      ZoomButtonsController zbc = mapView.getZoomButtonsController();
      ViewGroup container = zbc.getContainer();
      for (int i = 0; i < container.getChildCount(); i++) {
          View child = container.getChildAt(i);
          if (child instanceof ZoomControls) {
              FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) child.getLayoutParams();
              lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
              child.requestLayout();
              break;
          } 
      }
      

      【讨论】:

      • 最后。无需调用已弃用的 getZoomControls() 方法即可工作的答案。向你致敬。谢谢。
      • 非常好,一个更好的解决方案,它允许您保留内置的缩放控件以及淡入/淡出。谢谢!
      【解决方案4】:

      Reto:感谢您的回复,但我们的想法是不使用使用 XML 布局。

      我最终解决了这个问题。因为 MapView 是 ViewGroup 的子类,所以您可以轻松添加子视图(如缩放控件)。您只需要一个 MapView.LayoutParams 实例就可以了。我做了这样的事情(将缩放控件放在地图视图的底部中心)。

        // layout to insert zoomcontrols at the bottom center of a mapview
        MapView.LayoutParams params = MapView.LayoutParams(
          LayoutParams.WRAP_CONTENT,
          LayoutParams.WRAP_CONTENT,
          mapViewWidth / 2, mapViewHeight,
          MapView.LayoutParams.BOTTOM_CENTER);
      
        // add zoom controls
        mapView.addView(mapView.getZoomControls(), params);
      

      【讨论】:

        【解决方案5】:

        google groups thread 我发现了这个:

        没有 XML 的缩放控件:

           public class MyMapActivity extends MapActivity {  public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        
                RelativeLayout relativeLayout = new RelativeLayout(this);
                setContentView(relativeLayout);
        
                final MapView mapView = new MapView(this, DEBUG_MAP_API_KEY);
                RelativeLayout.LayoutParams mapViewLayoutParams = new
        RelativeLayout.LayoutParams
        (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT );
                relativeLayout.addView(mapView, mapViewLayoutParams);
        
                RelativeLayout.LayoutParams zoomControlsLayoutParams = new
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT );
                zoomControlsLayoutParams.addRule
        (RelativeLayout.ALIGN_PARENT_BOTTOM);
                zoomControlsLayoutParams.addRule
        (RelativeLayout.CENTER_HORIZONTAL);
        
                relativeLayout.addView(mapView.getZoomControls(),
        zoomControlsLayoutParams);
        
                mapView.setClickable(true);
                mapView.setEnabled(true);
        
         } 
        

        使用 SDK1.1 100% 为我工作

        【讨论】:

          【解决方案6】:

          很遗憾,我无法在 11 月 28 日 6:32 对 Jason Hudgins 批准的解决方案添加评论,但我的代码出现了一个小错误:

          在这一行:

          MapView.LayoutParams params = MapView.LayoutParams(
          

          Eclipse 给我的错误是

          "方法LayoutParams(int, int, int, int, int) 未定义 输入地图视图”

          相反,创建一个新的 MapView.LayoutParams 对象来修复它,如下所示:

          MapView.LayoutParams params = **new** MapView.LayoutParams(
          

          我花了一些时间才知道,因为我是 n00b :D

          【讨论】:

            【解决方案7】:

            你可以试试这个:

            MapView map = (MapView)findViewById(R.id.mapview);
            map.setBuiltInZoomControls(true);
            

            【讨论】:

              【解决方案8】:

              Reto - 使用 FILL_PARENT 的问题是缩放控件会“窃取”所有的触摸事件;这样您就无法在缩放控件可见时平移地图。你知道如何预防吗?

              【讨论】:

              • 你是对的。没有注意到这一点。用避免此问题的更好解决方案替换了先前的答案。
              猜你喜欢
              • 1970-01-01
              • 2010-10-29
              • 1970-01-01
              • 1970-01-01
              • 2012-06-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多