public void getDeviceLocation() {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(getMvpView().getActivity());
settingsClient = LocationServices.getSettingsClient(getMvpView().getActivity());
createLocationCallback();
createLocationRequest();
buildLocationSettingsRequest();
startLocationUpdates();
}
private void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void createLocationCallback() {
getMvpView().showProgressDialog();
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location currentLocation = locationResult.getLastLocation();
getMvpView().showSelectedAddress(getAddressFromLatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));
fusedLocationClient.removeLocationUpdates(locationCallback);
getMvpView().hideProgressDialog();
}
};
}
private void buildLocationSettingsRequest() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
locationSettingsRequest = builder.build();
}
private void startLocationUpdates() {
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(getMvpView().getActivity(), locationSettingsResponse -> {
if (ActivityCompat.checkSelfPermission(getMvpView().getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getMvpView().getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback, Looper.myLooper());
}).addOnFailureListener(getMvpView().getActivity(), e -> {
getMvpView().hideProgressDialog();
getMvpView().showErrorToast(R.string.please_enable_location);
});
}
@SuppressLint("MissingPermission")
private void startLocationUpdates() {
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(getMvpView().getActivity(), locationSettingsResponse -> {
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback, Looper.myLooper());
}).addOnFailureListener(getMvpView().getActivity(), e -> {
getMvpView().hideProgressDialog();
getMvpView().showErrorToast(R.string.please_enable_location);
});
}
private String getAddressFromLatLng(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(getMvpView().getActivity(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
latitude,
longitude,
1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addresses.get(0);
StringBuilder addressStringBuilder = new StringBuilder();
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressStringBuilder.append(address.getAddressLine(i));
}
return addressStringBuilder.toString();
}
I have converted the lat, lng to address text but after i query again for location the address changes even i have not moved from my current location, enabled accuracy in phone settings too. The issue is when i always query it should give me the same location address if my lat, lng postion is not changed.