【问题标题】:Google places API - V2- Autocomplete does not show all the place resultsGoogle 地方 API - V2 - 自动完成不显示所有地方结果
【发布时间】:2014-09-25 04:32:37
【问题描述】:

我正在尝试遵循 android 文档,但它并没有向我显示有关这些地方的所有建议。例如,如果我写集群 Z。这不会向我显示任何建议。我想要的正是如果我键入位置 X2,它应该返回我 X1、X2 本身、X3、X4、X5,因为我被这些位置包围。我错过了什么?

private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
ArrayList<String> resultList = null;
//API KEY = xxxxxxxxxxxxxxxxxxx; // Enter Your Key

private ArrayList<String> autocomplete(String input) {

    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        sb.append("?key=" + "Enter Your Key");
        sb.append("&components=country:myCountry");
        sb.append("&input=" + URLEncoder.encode(input, "utf8"));

        URL url = new URL(sb.toString());
        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");

        // Extract the Place descriptions from the results
        resultList = new ArrayList<String>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            resultList.add(predsJsonArray.getJSONObject(i).getString("description"));

        }
    } catch (JSONException e) {
        //Log.e(LOG_TAG, "Cannot process JSON results", e);
    }

    Log.v("returnResult",""+resultList.toString());
    return resultList;
}

【问题讨论】:

  • 你能给我地方键或用浏览器中的键检查 url.. url 和键是否正确
  • 我最近集成了它,它工作正常,你需要传递位置以获得准确的结果。
  • 按照这个教程可能会对你有所帮助。 wptrafficanalyzer.in/blog/…
  • 我想要完美的结果,我添加了位置、半径以获得更精确,但没有帮助。
  • @droid:已按照该教程进行操作。

标签: android google-places-api google-maps-api-2


【解决方案1】:

如果您想在 Google 地图搜索框中显示所有结果,请尝试使用 Google 搜索框而不是自动完成

【讨论】:

    【解决方案2】:

    如果您想用单个库方法调用替换所有这些代码,您可以查看Sprockets(披露:我是开发人员)。相当于:

    Places.autocomplete(Params.create().query(input)).getResult()
    

    Places Javadoc

    您还可以查看GooglePlaceAutoComplete 小部件,它为您完成所有工作并提供用户选择的Place

    <net.sf.sprockets.widget.GooglePlaceAutoComplete
        android:id="@+id/place"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    GooglePlaceAutoComplete Javadoc

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多