Oleg
Oleg

Reputation: 1358

Google maps android api: moving the compass button

I see that it's possible to set margins for the UI, also it's possible to disable the button.

But can we move compass button to a different location rather then top left?

Google maps app has it in the top right corner, which made me think that it's possible.

Upvotes: 6

Views: 3019

Answers (4)

Mostafa Pirhayati
Mostafa Pirhayati

Reputation: 433

I fixed the problem like this :

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

in this exemple i put the compass in the corner right. You need to be sure that your mapFragment is created when you do this, i suggest you run your code in the method "onMapReady" of your MapFragment.

Upvotes: 2

dante
dante

Reputation: 1254

On 2.0 map api.

    // change compass position 
if (mapView != null &&
        mapView.findViewById(Integer.parseInt("1")) != null) {
    // Get the view
    View locationCompass = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("5"));
    // and next place it, on bottom right (as Google Maps app)
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
            locationCompass.getLayoutParams();
    // position on right bottom
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    layoutParams.setMargins(0, 160,30, 0); // 160 la truc y , 30 la  truc x
}

Upvotes: 2

arne.z
arne.z

Reputation: 3576

I just had the same problem and solved it like this:

/* access compass */
ViewGroup parent = (ViewGroup) mMapView.findViewById(Integer.parseInt("1")).getParent();
View compassButton = parent.getChildAt(4);
/* position compass */
RelativeLayout.LayoutParams CompRlp = (RelativeLayout.LayoutParams) compassButton.getLayoutParams();
CompRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
CompRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
CompRlp.setMargins(0, 180, 180, 0);

This moves the compass a bit further away from the top

Upvotes: 3

ssawchenko
ssawchenko

Reputation: 1228

I am also struggling with this. My designers have pointed to Google Maps as an example, but I have yet to find a way to do this. The closest I have seen is JCDecary's response in this SO post which moves the buttons to the bottom, left corner. It depends on a named tag though, which makes me weary, as Google could decide to change that whenever.

Upvotes: 2

Related Questions