I'm trying to write an android application which tracks user's location. It works fine when permission is given to the app. I wrote a code piece to check permission and ask for it. But I'm having a problem when permission is given by the user; showing the location at that time. But it works find at the 2nd launch.
public class YourLocationActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap mMap;
String provider;
LocationManager locationManager;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_location);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
permissionAndLocationUpdate();
}
public void permissionAndLocationUpdate() {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION }, 1);
} else {
locationManager.requestLocationUpdates(provider, 400, 5, this);
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location"));
}
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1 && grantResults[0] == 0) {
fetchLastLocation();
} else {
}
}
private void fetchLastLocation(){
Task<Location> task = LocationServices.getFusedLocationProviderClient(this).getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location loc) {
if (location != null) {
mMap.addMarker(new MarkerOptions().position(new LatLng(loc.getLatitude(), loc.getLongitude())).title("Location"));
}else{
Toast.makeText(getApplicationContext(),"No Location recorded", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onLocationChanged(Location location) {
mMap.clear();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location"));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}