Change position of Google Maps API's "My location" button
Solution 1:
You can get the "My Location" button and move it, like :
public class MapFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mapView = super.onCreateView(inflater, container, savedInstanceState);
// Get the button view
View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2);
// and next place it, for exemple, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
}
}
Solution 2:
Just use GoogleMap.setPadding(left, top, right, bottom), which allows you to indicate parts of the map that may be obscured by other views. Setting padding re-positions the standard map controls, and camera updates will use the padded region.
https://developers.google.com/maps/documentation/android/map#map_padding
Solution 3:
This may not be the best solution, but you could place your own button over the map and handle it yourself. It would take the following:-
1) Put the map in a frameLayout and add your button on top. E.g.
<FrameLayout
android:id="@+id/mapFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapFragment"
map:mapType="normal"
map:uiCompass="true" />
<ImageButton
android:id="@+id/myMapLocationButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="bottom|right"
android:background="@drawable/myMapLocationDrawable"
android:contentDescription="My Location" />
</FrameLayout>
2) Edit the maps UI settings so the button doesn't appear when you call setMyLocationEnabled(true). You can do this via map.getUiSettings(). setMyLocationButtonEnabled(false);
3) Handle the click of your new button to emulate what the supplied button does. E.g. call mMap.setMyLocationEnabled(...); and pan the map to the current location.
Hope that helps, or hope someone comes a long with a simpler solution for you ;-)
Solution 4:
It's already been explained above.Just a small addition to fabLouis's answer. You may also get your map view from the SupportMapFragment.
/**
* Move the button
*/
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map);
View mapView = mapFragment.getView();
if (mapView != null &&
mapView.findViewById(1) != null) {
// Get the button view
View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2);
// and next place it, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
locationButton.getLayoutParams();
// position on right bottom
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.setMargins(0, 0, 30, 30);
}