将 AutoCompleteTextView 预测限制为仅餐厅

Restrict the AutoCompleteTextView predictions to only restaurants

我有一个 Android 应用程序,允许用户根据餐馆名称执行详细信息搜索。但是,根据用户的输入,预测可以包含地点、国家等。我可以在哪里添加限制以仅检查餐厅名称?

我当前的代码:

public static ArrayList<String> autocomplete(String input) {
        ArrayList<String> resultList = null;

        HttpURLConnection conn = null;
        StringBuilder jsonResults = new StringBuilder();
        try {
            StringBuilder sb = new StringBuilder(PLACES_API_BASE
                    + TYPE_AUTOCOMPLETE + OUT_JSON);
            sb.append("?key=" + API_KEY);
            sb.append("&input=" + URLEncoder.encode(input, "utf8"));
            URL url = new URL(sb.toString());
            System.out.println("URL: " + url);
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Load the results into a StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                jsonResults.append(buff, 0, read);
            }
        } catch (MalformedURLException e) {
            Log.e(LOG_TAG, "Error processing Places API URL", e);
            return resultList;
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error connecting to Places API", e);
            return resultList;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        try {
            // Create a JSON object hierarchy from the results
            JSONObject jsonObj = new JSONObject(jsonResults.toString());
            JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
            resultList = new ArrayList<String>(predsJsonArray.length());
            place = new HashMap<String, String>();
            for (int i = 0; i < predsJsonArray.length(); i++) {
//                  System.out.println(predsJsonArray.getJSONObject(i).getString(
//                          "description"));
//                  System.out
//                          .println("============================================================");
                    resultList.add(predsJsonArray.getJSONObject(i).getString(
                            "description"));

                 String description = predsJsonArray.getJSONObject(i).getString("description");
                 String placeId = predsJsonArray.getJSONObject(i).getString("place_id");
                 place.put( description, placeId);
            }
        } catch (JSONException e) {
            Log.e(LOG_TAG, "Cannot process JSON results", e);
        }
        return resultList;
    }

尝试在搜索中添加 place_type,使用此 link 查找支持的类型。

根据 place types,您可以获得的最接近的是 establishment 过滤器。您将必须执行搜索而不是自动完成才能使用 restaurant 过滤器。

使用 Places API for Android PlaceComplete 示例我尝试将 Place.TYPE_ESTABLISHMENTPlace.TYPE_RESTAURANT 传递给 AutocompleteFilter.create 来验证。