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

ぃ、小莉子 提交于 2019-12-13 03:46:13

问题


I'm trying to change the zoom & my location controls positions in Android Google maps.

I want to set zoom control position at the right bottom and set my location position at the left bottom of MapFragment (I'm using the com.google.android.gms.maps.SupportMapFragment).

Here is my map fragment layout:

<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"/>

The written codes for changing the controls position (getMapAsync callback):

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

}

But it doesn't work and I'd like to know what's wrong with my code. Is there a better solution for doing that?

[Updated - 1]

My application language is Persian (FA) so I've added android:supportsRtl="true" in the AndroidManifest.xml file in order to support RTL language.

Problem: The zoom control is aligned at the bottom left which I would reposition it at bottom right corner.


回答1:


You can find Location button on map by getting parent view first and then "My Location" button as below :

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

If want to resize button, you can get parameters like below :

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



回答2:


This code (based on 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();
    }
}

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

and 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;
        }
    }
}

worked for me:

without any android:supportsRtl="true" settings.



来源:https://stackoverflow.com/questions/47777627/changing-the-position-of-google-maps-supportmapfragment-zoom-and-my-location-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!