Android Maps Library V2 zoom controls custom posit

2020-02-23 06:17发布

问题:

How can i customize the position of the builtin zoom controls in a GoogleMap V2 ?

There are a lot of questions related to this topic for the Version 1 of Google Maps library.

Placing Zoom Controls in a MapView

How to reposition built-in zoom controls in MapView?

How to layout zoom Control with setBuiltInZoomControls(true)?

However, i wasn't able to find any questions in relation to the V2 of the library.

In the V2, there's no method

(LinearLayout) mapView.getZoomControls();

all the previously mentioned questions becomes obsolete.

Thanks in advance

回答1:

Depending on what you're trying to do, you might find GoogleMap.setPadding() useful (added in September 2013).

map.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);

From the API docs:

This method allows you to define a visible region on the map, to signal to the map that portions of the map around the edges may be obscured, by setting padding on each of the four edges of the map. Map functions will be adapted to the padding. For example, the zoom controls, compass, copyright notices and Google logo will be moved to fit inside the defined region, camera movements will be relative to the center of the visible region, etc.

Also see the description of how padding works in GoogleMap.



回答2:

Yes, you can change position of ZoomControl and MyLocation button with small hack. In my sample I have SupportMapFragment, which is inflated from xml layout.

View ids for ZoomControl and MyLocation button:

ZoomControl id = 0x1
MyLocation button id = 0x2

Code to update ZoomControl position:

// Find map fragment
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

// Find ZoomControl view
View zoomControls = mapFragment.getView().findViewById(0x1);

if (zoomControls != null && zoomControls.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
    // ZoomControl is inside of RelativeLayout
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) zoomControls.getLayoutParams();

    // Align it to - parent top|left
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    // Update margins, set to 10dp
    final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
            getResources().getDisplayMetrics());
    params.setMargins(margin, margin, margin, margin);
}


回答3:

Just for the record:

Navigation-controls are 0x4

So in total:

@LayoutRes final int ZOOM_CONTROL_ID = 0x1;
@LayoutRes final int MY_LOCATION_CONTROL_ID = 0x2;
@LayoutRes final int NAVIGATION_CONTROL_ID = 0x4;


回答4:

I use MapFragment not SupportMapFragment:

import android.util.TypedValue;

in onCreate

    // Find map fragment

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapview);

    int ZoomControl_id = 0x1;
    int MyLocation_button_id = 0x2;

    // Find ZoomControl view
    View zoomControls = mapFragment.getView().findViewById(ZoomControl_id);

    if (zoomControls != null && zoomControls.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
        // ZoomControl is inside of RelativeLayout
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) zoomControls.getLayoutParams();

        // Align it to - parent top|left
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        // Update margins, set to 10dp
        final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
                getResources().getDisplayMetrics());
        params.setMargins(margin, margin, margin, margin);
    }