Reputation:
I am writing an application in android using Google map-V2 API. I want to over lay action bar just as in the Google map application. And I enabled "My Location " button. The problem now is that my location button is under the action bar. Is there any way to re-position this button. I want to make an app some what similar to Maps. I am new to android so please help.
Upvotes: 12
Views: 17430
Reputation: 3983
Instead of the findViewById solutions, I used the findViewWithTag to get a reference to the button. Using a string seemed more readable and reliable to me.
View myLocationButton = mMap.findViewWithTag("GoogleMapMyLocationButton");
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) myLocationButton.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
Upvotes: 3
Reputation: 1023
I solved this problem in my map fragment by re positioning my location button to the right bottom corner of view using code below, here is my Maps Activity.java :-
add this lines of code in onCreate() method,
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapView = mapFragment.getView();
mapFragment.getMapAsync(this);
and here is onMapReady() code :-
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
if (mapView != null &&
mapView.findViewById(Integer.parseInt("1")) != null) {
// Get the button view
View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("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);
}
}
I hope, this will solve your problem. Thanks.
Upvotes: 3
Reputation: 1850
You can reposition your location button easily
View plusMinusButton = suppormanagerObj.getView().findViewById(1);
View locationButton = suppormanagerObj.getView().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);
you can also set padding after setting alignment of the location button
mMap.setPadding(0, 0, 30, 105);
Upvotes: 9
Reputation: 914
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
Upvotes: 2
Reputation: 1727
You can set padding to the map.
This solved the problem - overlays over the map on the top (I used 48 dips, but you can overlay action bar and then get actual height of it (?android:attr/actionBarSize))
mapFragment.getMap().setPadding(0, dpToPx(48), 0, 0);
(DP to PX function is from this SO answer)
Upvotes: 14
Reputation: 12933
You can not alter the MyLocationButton in any way, but enable and disable it. There is already a request for this.
Feel free to disable the button and just implement your own one.
You would have something like this:
mMap.getUiSettings().setMyLocationButtonEnabled(false);
Upvotes: 8