【问题标题】:Change the position of compass in mapview [duplicate]更改地图视图中指南针的位置[重复]
【发布时间】:2020-06-17 20:09:35
【问题描述】:

我需要在应用程序中将指南针的位置从左到右更改。它总是显示在左侧。是否可以更改位置??

  // change compass position
    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);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                        rlp.rightMargin = rlp.leftMargin;
                        rlp.topMargin = 25;
                        view.requestLayout();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    这是对齐右上角的指南针按钮所需的解决方案:

    View compassButton = mMapView.findViewWithTag("GoogleMapCompass");//to access the compass button
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) compassButton.getLayoutParams();
    rlp.addRule(RelativeLayout.ALIGN_PARENT_END);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_START,0);
    rlp.topMargin = 50;
    

    这将更改它的 RelativeLayout 参数以与右上角对齐。

    结果:

    【讨论】:

    • 当我们想要将其与右上角对齐时,您的代码是否有效?我认为这是个问题
    • 如果您可以将指南针按钮放在右上角会很棒,因为我也尝试过但无法将指南针按钮对齐到右上角,是的,我已经能够将它对齐到左下角.
    • 真的很感谢这位老兄,谢谢
    • @RahulChandrabhan 检查更新。
    • 它的工作,为你+1
    【解决方案2】:

    您可以使用 GoogleMap.setPadding() 方法:用于您的指南针位置

    https://developers.google.com/maps/documentation/android/map#map_padding

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      相关资源
      最近更新 更多