Restrict Places Autocomplete API to cities of specific country android

前端 未结 4 1870
余生分开走
余生分开走 2021-02-13 12:49

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 In

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 13:30

    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.

提交回复
热议问题