【问题标题】:Google maps android api: moving the compass buttonGoogle maps android api:移动指南针按钮
【发布时间】:2018-01-14 17:10:44
【问题描述】:

我看到可以为 UI 设置边距,也可以禁用按钮。

但是我们可以将指南针按钮移动到其他位置而不是左上角吗?

谷歌地图应用在右上角有它,这让我觉得这是可能的。

【问题讨论】:

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


    【解决方案1】:

    我刚刚遇到了同样的问题,然后这样解决了:

    /* access compass */
    ViewGroup parent = (ViewGroup) mMapView.findViewById(Integer.parseInt("1")).getParent();
    View compassButton = parent.getChildAt(4);
    /* position compass */
    RelativeLayout.LayoutParams CompRlp = (RelativeLayout.LayoutParams) compassButton.getLayoutParams();
    CompRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
    CompRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    CompRlp.setMargins(0, 180, 180, 0);
    

    这会使指南针离顶部更远一点

    【讨论】:

      【解决方案2】:

      我也在为此苦苦挣扎。我的设计师以谷歌地图为例,但我还没有找到一种方法来做到这一点。我见过的最接近的是 JCDecary 在this SO post 中的响应,它将按钮移动到左下角。不过它依赖于一个命名标签,这让我感到厌烦,因为 Google 可以决定随时更改它。

      【讨论】:

        【解决方案3】:

        关于 2.0 地图 api。

            // change compass position 
        if (mapView != null &&
                mapView.findViewById(Integer.parseInt("1")) != null) {
            // Get the view
            View locationCompass = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("5"));
            // and next place it, on bottom right (as Google Maps app)
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                    locationCompass.getLayoutParams();
            // position on right bottom
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            layoutParams.setMargins(0, 160,30, 0); // 160 la truc y , 30 la  truc x
        }
        

        【讨论】:

          【解决方案4】:

          我解决了这样的问题:

          try {
                          assert mapFragment.getView() != null;
                          final ViewGroup parent = (ViewGroup) mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
                          parent.post(new Runnable() {
                              @Override
                              public void run() {
                                  try {
                                      for (int i = 0, n = parent.getChildCount(); i < n; i++) {
                                          View view = parent.getChildAt(i);
                                          RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams();
                                          // position on right bottom
                                          rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
                                          rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                                          rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                                          rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                                          rlp.rightMargin = rlp.leftMargin;
                                          rlp.bottomMargin = 25;
                                          view.requestLayout();
                                      }
                                  } catch (Exception ex) {
                                      ex.printStackTrace();
                                  }
                              }
                          });
                      } catch (Exception ex) {
                          ex.printStackTrace();
                  }
          

          在这个例子中,我把指南针放在右边的角落里。您需要确保在执行此操作时创建了您的 mapFragment,我建议您在 MapFragment 的“onMapReady”方法中运行您的代码。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-08-04
            • 1970-01-01
            • 2013-01-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多