I started developing an app using google maps and it worked just fine. The map showed the current device location. Then I tried changing the map type and it worked...kinda.. The device's location doesn't show up anymore. So I deleted the line changing the map type. But the current location still isn't visible. Does anybody know why?
public class Map extends FragmentActivity implements OnMapReadyCallback {
GoogleMap map;
CameraPosition cameraPosition;
private Location lastKnownLocation;
private boolean locationPermissionGranted;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private static final String TAG = "--LOG--";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
getLocationPermission();
}
private void getLocationPermission(){
if (ContextCompat.checkSelfPermission(this.getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{ locationPermissionGranted = true; }
else {
//Wenn die Berechtigung nicht erteilt wurde, wird nachgefragt
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); }
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
locationPermissionGranted = false;
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationPermissionGranted = true;
}
}
}
updateDeviceLocation();
}
private void updateDeviceLocation() {
if (map == null) {
return;
}
try {
if (locationPermissionGranted) {
//wenn standortzugriff erlaubt ist, wird der standort genutzt & angezeigt
map.setMyLocationEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);}
else {
// wenn die Berechtigung verweigert wurde eine erneute Anfrage
map.setMyLocationEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
lastKnownLocation = null;
getLocationPermission(); }
}
catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}
``
question from:https://stackoverflow.com/questions/65643821/maps-doesnt-show-the-current-location-anymore