2015-05-01 58 views
0

我有一個Android應用程序,允許用戶根據餐館名稱執行詳細搜索。但是,根據用戶的輸入,預測可能包含地點,國家等。我可以在哪裏添加限制以僅檢查餐館名稱?將AutoCompleteTextView預測僅限於餐廳

我當前的代碼:

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; 
    } 
+0

爲此問題添加星標,[允許在自動填充中添加地點類型](https://code.google.com/p/gmaps-api-issues/issues/detail?id=3538),如果您希望看到此增強功能。 – pushbit

回答

1

根據place types最接近你可以得到的是establishment過濾器。您將不得不執行搜索而不是自動完成,以便使用restaurant過濾器。

使用Places API for Android PlaceComplete示例我試着通過Place.TYPE_ESTABLISHMENTPlace.TYPE_RESTAURANTAutocompleteFilter.create來驗證。

0

嘗試在你的搜索添加place_type,使用此link找到支持的類型。