This is how I enable my location button so it can be clicked on the map:
map.setMyLocationEnabled(true);
My question is, how to find this button and store it in a variable so it can be displayed or not according to the logic of my app? Thanks!
This is how I enable my location button so it can be clicked on the map:
map.setMyLocationEnabled(true);
My question is, how to find this button and store it in a variable so it can be displayed or not according to the logic of my app? Thanks!
You can get it via findViewWithTag("GoogleMapMyLocationButton")
on MapFragment
root view. With code like this:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
...
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
private View mMyLocationButtonView = null;
...
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String [] {Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
} else {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mMyLocationButtonView = mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton");
mMyLocationButtonView.setBackgroundColor(Color.GREEN);
}
}
}
}
you'll got something like that:
You can prevent the My Location button from appearing by calling UiSettings.setMyLocationButtonEnabled(false)
.
See https://developers.google.com/maps/documentation/android-sdk/location#my-location