I think this will give a better result:
private String convertLocationToAddress(Location location) {
String addressText;
String errorMessage = "";
Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1
);
} catch (IOException ioException) {
// Network or other I/O issues
errorMessage = getString(R.string.network_service_error);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Invalid long / lat
errorMessage = getString(R.string.invalid_long_lat);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// No address was found
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
addressText = errorMessage;
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<>();
// Fetch the address lines, join them, and return to thread
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
addressText =
TextUtils.join(System.getProperty("line.separator"),
addressFragments);
}
return addressText;
}