I am working on the app that tracks user live location, and I am using FusedLocationProviderClient to get the current location but it return the same location sometimes i.e app gets a location when app is opened, after moving to another place say different village when app tries to get new location it is getting the previous location again...
Please help
My code is as following.
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
locationRequest = LocationRequest.create();
locationRequest.setInterval(MIN_UPDATE_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
protected void onResume() {
super.onResume();
try {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int result = googleApiAvailability.isGooglePlayServicesAvailable(this);
if (result != ConnectionResult.SUCCESS && result != ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) {
Toast.makeText(this, "Are you running in Emulator ? try a real device.", Toast.LENGTH_SHORT).show();
}
callCurrentLocation(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void callCurrentLocation(final int type) {
try {
if (
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
) {
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
requestPermissions(REQUEST_PERMISSIONS_CURRENT_LOCATION_REQUEST_CODE);
return;
}
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
currentLocation = (Location) locationResult.getLastLocation();
if (currentLocation != null) {
latitude = currentLocation.getLatitude();
longitude = currentLocation.getLongitude();
currentAccuracy = currentLocation.getAccuracy();
}
String result = "Current Location Latitude is " +
currentLocation.getLatitude() + "\n" +
"Current location Longitude is " + currentLocation.getLongitude() + " \n Accuracy is " + currentLocation.getAccuracy();
Toast.makeText(act, "current Location : " + result, Toast.LENGTH_SHORT).show();
}
};
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper());
} catch (Exception ex) {
ex.printStackTrace();
}
}