在mapview中更改指南针的位置

时间:2018-03-16 03:58:14

标签: android google-maps

enter image description here

我需要在app中从左到右改变指南针的位置。它总是在左边显示。是否可以改变位置?

  // 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();
    }

2 个答案:

答案 0 :(得分:1)

这是所需的解决方案:

<强>更新

View compassButton = mMapView.findViewWithTag("GoogleMapCompass");//this works for me
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;

Screenshot for the updated location of the button

答案 1 :(得分:0)

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

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