更改 google 地图的位置 SupportMapFragment 缩放和我的位置控件不起作用

changing the position of google maps SupportMapFragment Zoom and My location controls not work

我正在尝试更改 Android Google 地图中的缩放和我的位置控件位置。

我想将缩放控制位置设置在右下角,并将我的定位位置设置在MapFragment的左下角(我使用的是com.google.android.gms.maps.SupportMapFragment)。

这是我的地图片段布局:

<fragment
       android:id="@+id/map"
        android:layout_below="@+id/toolbar"
       class="com.google.android.gms.maps.SupportMapFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

更改控件位置(getMapAsync回调)的编写代码:

public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
    if (map != null) {
        // Map is ready

        map.getUiSettings().setZoomControlsEnabled(true);

        View mapView = mapFragment.getView();

        // Zoom control
        @SuppressLint("ResourceType") View zoomButton = mapView.findViewById(0x1);

        if (zoomButton != null && zoomButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {

            RelativeLayout.LayoutParams zp = (RelativeLayout.LayoutParams) zoomButton.getLayoutParams();

            zp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            zp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

            zp.setMargins(0, 0, 30, 30);
        }

        // My location control
        @SuppressLint("ResourceType") View locationButton = mapView.findViewById(0x2);

        if (locationButton != null && locationButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();

        // position on left bottom
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

            lp.setMargins(30, 0, 0, 30);
    }

}

但是它不起作用,我想知道我的代码有什么问题。有更好的解决方案吗?

[已更新 - 1]

我的应用程序语言是波斯语 (FA),所以我在 AndroidManifest.xml 文件中添加了 android:supportsRtl="true" 以支持 RTL 语言。

问题:缩放控件在左下角对齐,我会将其重新定位在右下角。

您可以通过先获取父视图然后获取 "My Location" 按钮来在地图上找到位置按钮,如下所示:

// Get the "My Location" button view 
View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2);

// get button params to place the button
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();

// place on right bottom corner
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);

如果要调整按钮的大小,您可以获取如下参数:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 100);
//.... do your editing
// To set params to button
locationButton.setLayoutParams(lp);

此代码(基于that answer of Vignon):

void setConrolsPositions() {
    try {
        mGoogleMap.setMyLocationEnabled(true);
        mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);

        final ViewGroup parent = (ViewGroup) mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
        parent.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Resources r = getResources();
                    //convert our dp margin into pixels
                    int marginPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
                    // Get the map compass view

                    View mapLocation = mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton");

                    // create layoutParams, giving it our wanted width and height(important, by default the width is "match parent")
                    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(mapLocation.getHeight(),
                            mapLocation.getHeight());
                    // position on top right
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    //give compass margin
                    rlp.setMargins(marginPixels, marginPixels, marginPixels, marginPixels);
                    mapLocation.setLayoutParams(rlp);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

来自 onMapReady():

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        if (locationPermission != PackageManager.PERMISSION_GRANTED) {
            setConrolsPositions();
        } else {
            setConrolsPositions();
        }
    } else {
        setConrolsPositions();
    }
}

onRequestPermissionsResult():

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case LOCATION_PERMISSION_REQUEST_CODE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setConrolsPositions();
            } else {
            }
            return;
        }
    }
}

对我有用:

没有任何 android:supportsRtl="true" 设置。