I'm updating my code with permissions from the new Android API 23
. If I want to show user's location I just have to map.setMyLocationEnabled(true)
. If I have the permission I can see user's location, if I don't have permission I don't make this API call but after requesting the permissions and user accepts giving Location permissions I can't update the map with user's location. I've tried 2 approaches:
Approach 1 (rebuild map):
private void populateMap() {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
...
if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
map.setMyLocationEnabled(true);
} else {
request permissions...
}
...
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
populateMap();
}
return;
}
}
}
Approach 2 (use map instance and update it):
private void populateMap() {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
...
if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
map.setMyLocationEnabled(true);
} else {
request permissions...
}
...
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
if (mMap!= null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
return;
}
}
}
In both approaches the location button is shown, but the blue dot with user's location doesn't. What am I missing here? Thanks.
ps: Of course If I kill my Fragment and launch it again everything works fine, this issue happens when I have to prompt user for permissions and then update the map with my location after user's authorization.
So, I was testing with Genymotion
because I couldn't get my hands on a device with Android
6.0 but now, that I've borrowed one, I've tested this code in that physical device and I was happy to notice that it works.
Guess it's a Genymotion
problem. Oh well, code's working.
Thanks!