【发布时间】: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