Reputation: 7936
I'm doing an app, with google maps, but when I try to add "my-location" button, as the reference says doesn't work...
thats how I do:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
locManager = (LocationManager)getSystemService(LOCATION_SERVICE);
providersList = locManager.getAllProviders();
provider =locManager.getProvider(providersList.get(0));
precision = provider.getAccuracy();
req = new Criteria();
req.setAccuracy(Criteria.ACCURACY_FINE);
inside = false;
map.getUiSettings().setMyLocationButtonEnabled(true);
buildPolygon();
drawPolygon();
startLocalization();
}
I used map.getUiSettings().setMyLocationButtonEnabled(true);
as shows in the reference of google. I don't know what's going on..
Upvotes: 33
Views: 30407
Reputation: 797
You need permission to make it visible
if (ActivityCompat.checkSelfPermission(Maps_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(Maps_Activity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermission();
return;
}
mMap.setMyLocationEnabled(true);
Upvotes: 0
Reputation: 969
Add the following line.
Kotlin:
map.isMyLocationEnabled = true
map
is a GoogleMap object.
(More detailed answer(s) in this question: https://stackoverflow.com/a/15142091/11211963 )
Upvotes: 2
Reputation: 28773
The above answers didn't work.
override fun onMapReady(googleMap: GoogleMap?) {
this.googleMap = googleMap
setupMap()
}
private fun setupMap() {
if (ActivityCompat.checkSelfPermission(context!!,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// Before enabling the My Location layer, you must request location permission from the user.
googleMap?.isMyLocationEnabled = true
// *** Use this method ***
googleMap?.uiSettings?.isMyLocationButtonEnabled = true
// See https://developers.google.com/maps/documentation/android-sdk/location
googleMap?.setOnMyLocationButtonClickListener(this)
googleMap?.setOnMyLocationClickListener(this)
} else {
// Show rationale and request permission.
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
LOCATION_REQUEST_CODE)
}
}
Upvotes: 0
Reputation: 1540
For me, map.setMyLocationEnabled(true);
It worked fine for below marshmallow devices and above marshmallow devices, I gave Location permission manually in-app settings.
Later My Location Button got visible.
Upvotes: 0
Reputation: 21452
just add map.setMyLocationEnabled(true);
after create your map
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
to be like this
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
Upvotes: 3
Reputation: 8925
The myLocationButtonEnabled
is true by default and shown when the setMyLocationEnabled
layer is enabled.
try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Add this line
map.setMyLocationEnabled(true);
locManager = (LocationManager)getSystemService(LOCATION_SERVICE);
providersList = locManager.getAllProviders();
provider =locManager.getProvider(providersList.get(0));
precision = provider.getAccuracy();
req = new Criteria();
req.setAccuracy(Criteria.ACCURACY_FINE);
inside = false;
//map.getUiSettings().setMyLocationButtonEnabled(true);
buildPolygon();
drawPolygon();
startLocalization();
}
setMyLocationEnabled
Documentation
setMyLocationButtonEnabled
Documentation
Upvotes: 57