Changing The Position Of Google Maps Supportmapfragment Zoom And My Location Controls Not Work
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 t
Solution 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 ViewlocationButton= ((View) mapView.findViewById(1).getParent()).findViewById(2);
// get button params to place the button
RelativeLayout.LayoutParamslp= (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.LayoutParamslp=newRelativeLayout.LayoutParams(100, 100);
//.... do your editing// To set params to button
locationButton.setLayoutParams(lp);
Solution 2:
This code (based on that answer of Vignon) :
voidsetConrolsPositions() {
try {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
finalViewGroupparent= (ViewGroup) mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
parent.post(newRunnable() {
@Overridepublicvoidrun() {
try {
Resourcesr= getResources();
//convert our dp margin into pixelsintmarginPixels= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
// Get the map compass viewViewmapLocation= mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton");
// create layoutParams, giving it our wanted width and height(important, by default the width is "match parent")
RelativeLayout.LayoutParamsrlp=newRelativeLayout.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()
:
@OverridepublicvoidonMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intlocationPermission= ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
setConrolsPositions();
} else {
setConrolsPositions();
}
} else {
setConrolsPositions();
}
}
and onRequestPermissionsResult()
:
@OverridepublicvoidonRequestPermissionsResult(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.
Post a Comment for "Changing The Position Of Google Maps Supportmapfragment Zoom And My Location Controls Not Work"