Restrict Places Autocomplete API to cities of specific country android

杀马特。学长 韩版系。学妹 提交于 2019-12-21 03:37:11

问题


I am using PlaceAutocompleteApi from Play Services, what I want to do is restrict auto-complete to specific country & city only. Eg. All cities from lets say India only. I am using AutocompleteFilter to do so but I don't know where to specify country that I want to restrict.

Here is my code

        List<Integer> autocompleteFilter = new ArrayList<>();
//        autocompleteFilter.add(Place.TYPE_COUNTRY);
//        autocompleteFilter.add(Place.TYPE_LOCALITY);


        mAdapter = new PlaceAutocompleteAdapter(mContext,R.layout.layout_location_text,
                mGoogleApiClient, BOUNDS, AutocompleteFilter.create(autocompleteFilter));
        mTxtLocation.setAdapter(mAdapter);

        mTxtLocation.setOnItemClickListener(mAutocompleteClickListener);

any thoughts on this?


回答1:


The way to do that is to add the country as the components parameter to the web service URL:

StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&components=country:in");

For cities you can use either the administrative_area_level_3 tag:

sb.append("&components=administrative_area_level_3:bengaluru");

or alternatively the locality tag:

sb.append("&components=locality:redmond");

References:

1. Geocoding API.

2. Address Types and Component Types.

3. Google Place API Autocomplete Service in Android Application.




回答2:


Here's a workaround (hack if you will) that I used. It utilizes (exploits) the fact that when converted to string, each response item is a comma separated string. Do the following steps before returning the result to getFilter().

  1. Get the suggestions into an ArrayList, call it returnedResults.
  2. Now traverse over returnedResults and in each iteration get each prediction into a string array using returnedResults.get(i).getFullText(xxx).toString().split(",").
  3. If the last element of this array is your country's name, insert it in a separate ArrayList variable, let's call it myResult.
  4. return myResult.

The reason for using separate variables is because you might not be able to manipulate the original response list so easily. This approach works like a charm!

Hope this helps.




回答3:


Filter results by country

// Create Filter
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
    .setCountry("AU")
    .build();
// Attach to autocomplete fragment / widget
autocompleteFragment.setFilter(typeFilter);



回答4:


val sb = StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON)
            sb.append("?key=$API_KEY")
            sb.append("&types=establishment")
            sb.append("&location=$locationLat,$LocationLng")
            sb.append("&radius=50000&strictbounds")
            sb.append("&input=" + URLEncoder.encode(input, "utf8"))

This api will restrict the search to area.



来源:https://stackoverflow.com/questions/30909331/restrict-places-autocomplete-api-to-cities-of-specific-country-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!