2

How to restrict a country using the geocoder?. I used this code, trying to just look at U.S., but it returns results from Italy and other countries.

Geocoder geocoder = new Geocoder (getActivity(), Locale.US);

I have search, but i didn't found anything usefull.

moskis
  • 1,136
  • 10
  • 20

2 Answers2

4

I solved this recently by simply appending ", USA" to the end of the user's input. Works perfectly for our needs with just a few lines of code:

public String restrictQueryToUsa(@NonNull String query) {
    String suffix = ", USA";
    return query.toUpperCase().endsWith( suffix ) ? query : query + suffix;
}

Note
I've verified that this on a variety of inputs including:

  • Buckingham Palace
  • 1234 Main Street
  • Holland
  • London
  • Tokyo
  • McDonald's
  • Zip codes like 90210 work
  • Airport codes like SFO also work
gMale
  • 17,147
  • 17
  • 91
  • 116
1

You should implement getFilter method in your AutoComplete Adapter which you apply on your AutoCompleteTextView, you should also implement "Filterable"

private class AutoCompleteAdapter extends ArrayAdapter<Address> implements
            Filterable {
.....

@Override
        public Filter getFilter() {
            Filter myFilter = new Filter() {
                @Override
                protected FilterResults performFiltering(
                        final CharSequence constraint) {
                    List<Address> addressList = null;
                    if (constraint != null) {
                        try {
                            addressList = mGeocoder.getFromLocationName(
                                    (String) constraint, 5);
                        } catch (IOException e) {
                        }
                    }
                    if (addressList == null) {
                        addressList = new ArrayList<Address>();
                    }

                    final FilterResults filterResults = new FilterResults();
                    filterResults.values = addressList;
                    filterResults.count = addressList.size();

                    return filterResults;
                }

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(final CharSequence contraint,
                        final FilterResults results) {
                    clear();
                    for (Address address : (List<Address>) results.values) {
                        try {


/* HERE IS YOUR COUNTRY CODE / CONDITIONS TO FILTER THE SUGGESTED ADDRESS*/


                            if (address.getCountryCode().equals("US")) { 
                                add(address);
                            }
                        } catch (Exception e) {
                            // TODO: handle exception
                        }

                    }
                    if (results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }

                @Override
                public CharSequence convertResultToString(
                        final Object resultValue) {
                    return resultValue == null ? ""
                            : createFormattedAddressFromAddress((Address) resultValue);
                }
            };
            return myFilter;
        }
}
Idan Gozlan
  • 3,173
  • 3
  • 30
  • 47