I'm getting address using Geocoder. Sometimes I get city or postcode twice. I want to remove these duplicate words to make the address more acceptable. Is there any way to do this? I searched for removing duplicate words from string but only got one good result but it didn't worked in my case. From the below code if I fetch my current address, I get result:
Street no 0360
DH Block(Newtown), Action Area I, Newtown
New Town, West Bengal 700156
New Town
700156
India
Code
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
String result = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
} catch (IOException ioException) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +"Latitude = " + location.getLatitude() +", Longitude = " + location.getLongitude(), illegalArgumentException);
}
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(AppUtils.LocationConstants.FAILURE_RESULT, errorMessage, null);
} else {
Address address = addresses.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append(" ");
}
sb.append(address.getLocality()).append(" ");
sb.append(address.getPostalCode()).append(" ");
sb.append(address.getCountryName());
result = sb.toString();
}