【问题标题】:Android Maps Library V2 zoom controls custom positionAndroid Maps Library V2 缩放控件自定义位置
【发布时间】:2012-12-13 19:41:21
【问题描述】:

如何自定义 GoogleMap V2 中内置缩放控件的位置?

Google 地图库第 1 版有很多与此主题相关的问题。

Placing Zoom Controls in a MapView

How to reposition built-in zoom controls in MapView?

How to layout zoom Control with setBuiltInZoomControls(true)?

但是,我找不到任何与图书馆 V2 相关的问题。

在V2中,没有方法

(LinearLayout) mapView.getZoomControls();

前面提到的所有问题都已过时。

提前致谢

【问题讨论】:

    标签: android google-maps google-maps-android-api-2


    【解决方案1】:

    根据您的尝试,您可能会发现GoogleMap.setPadding() 很有用(2013 年 9 月添加)。

    map.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
    

    来自 API 文档:

    此方法允许您在地图上定义一个可见区域,通过在地图的四个边缘中的每一个上设置填充来向地图发出信号,表明边缘周围的地图部分可能会被遮挡。地图功能将适应填充。例如,缩放控件、指南针、版权声明和 Google 徽标将被移动以适应定义的区域,相机移动将相对于可见区域的中心等。

    另见description of how padding works in GoogleMap

    【讨论】:

      【解决方案2】:

      是的,您可以通过小技巧改变 ZoomControlMyLocation 按钮的位置。 在我的示例中,我有 SupportMapFragment,它是从 xml 布局扩展而来的。

      查看 ZoomControl 和 MyLocation 按钮的 ID:

      ZoomControl id = 0x1
      MyLocation button id = 0x2
      

      更新 ZoomControl 位置的代码:

      // Find map fragment
      SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
      
      // Find ZoomControl view
      View zoomControls = mapFragment.getView().findViewById(0x1);
      
      if (zoomControls != null && zoomControls.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
          // ZoomControl is inside of RelativeLayout
          RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) zoomControls.getLayoutParams();
      
          // Align it to - parent top|left
          params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
          params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
      
          // Update margins, set to 10dp
          final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
                  getResources().getDisplayMetrics());
          params.setMargins(margin, margin, margin, margin);
      }
      

      【讨论】:

      • 如何保证mapFragment.getView().findViewById(0x1);总是会返回带有缩放控件的RelativeLayout?据我所知,这通常是 android 工具在生成 R 类时生成的随机整数。我错过了什么吗?
      • 因为他们明确地做了 ZoomControl.setId(1) 和 MyLocationButton.setId(2)。所以它应该可以正常工作,直到他们更改 ID。还有第三个控件,0x3,但我不确定它是干什么用的。
      • 感谢您的回答。不过,我会保留已接受的答案,因为这是一个 hack,并且没有文档支持。
      • 这非常脆弱。这些值可以随任何 Maps V2 版本而改变。请简单地隐藏内置缩放控件并添加您自己的。
      • 您好vovkab,是否可以重新定位(即低于50)地图v2的指南针按钮?如果可能,请给予回应。
      【解决方案3】:

      我使用 MapFragment 而不是 SupportMapFragment:

      import android.util.TypedValue;
      

      在创建时

          // Find map fragment
      
          MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapview);
      
          int ZoomControl_id = 0x1;
          int MyLocation_button_id = 0x2;
      
          // Find ZoomControl view
          View zoomControls = mapFragment.getView().findViewById(ZoomControl_id);
      
          if (zoomControls != null && zoomControls.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
              // ZoomControl is inside of RelativeLayout
              RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) zoomControls.getLayoutParams();
      
              // Align it to - parent top|left
              params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
              params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
      
              // Update margins, set to 10dp
              final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
                      getResources().getDisplayMetrics());
              params.setMargins(margin, margin, margin, margin);
          }
      

      【讨论】:

      • 谢谢!这正是我所需要的。 SO上的其他解决方案要么已弃用,要么对我不起作用。我不得不改用zoomControls.setPadding(20,0,0,20);,因为margin 不起作用,不知道为什么。
      【解决方案4】:

      仅作记录:

      导航控件是0x4

      总之:

      @LayoutRes final int ZOOM_CONTROL_ID = 0x1;
      @LayoutRes final int MY_LOCATION_CONTROL_ID = 0x2;
      @LayoutRes final int NAVIGATION_CONTROL_ID = 0x4;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 2014-01-12
        • 1970-01-01
        • 2012-12-05
        • 1970-01-01
        • 2013-09-09
        • 2013-06-04
        相关资源
        最近更新 更多