公共类PlacesAutoCompleteAdapter extends ArrayAdapter实现了Filterable {
private static final String LOG_TAG = "Roomys";
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";
private static final String API_KEY = "Akalkcalckalaxlxxkalxii22leilkala";
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
Log.d("getFilter","constraint is "+constraint);
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
Log.d("publishing results","true");
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
private 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"));
Log.i("search api uri", sb.toString());
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();
}
}
if(jsonResults!=null){
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"));
Log.d("places auto","description is "+predsJsonArray.getJSONObject(i).getString("description"));
Log.d("places auto","result list size is "+resultList.size());
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
}
return resultList;
}
}
使用代码
actv = (AutoCompleteTextView) dialog.findViewById(R.id.home_address_act);
PlacesAutoCompleteAdapter adapter = new PlacesAutoCompleteAdapter(getActivity(), R.layout.autocomplete_list_item);
adapter.setNotifyOnChange(true);
actv.setAdapter(adapter);
actv.setOnItemClickListener(this);
获取文字。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
address = (String) parent.getItemAtPosition(position);
Log.d("places autocomplete","selected place is "+address);
}
请帮助我,这是一个问题,因为我也想从这个地方计算lat n long并且没有出现我要进入的地方。但我的朋友的PHP网站谷歌地方工作正常。这是android
的问题这里是正确显示1个预测的json响应。
{
"predictions" : [
{
"description" : "UBCI Gremda, C81, Sfax, Tunisia",
"id" : "f4c3384175d71788cbfd6ff7ee1809b893d55dbf",
"matched_substrings" : [
{
"length" : 7,
"offset" : 0
}
],
"place_id" : "ChIJy5CYbYTTARMRLlJ_ciDn0FA",
"reference" : "CjQtAAAAdA8G1_eaEc6ojJye079YMzIGLTW8h14KpcERlGzD8RYR4LWM9asStZFqNm2kRq7QEhCB9ge0-NYjo15-2304DKG0GhQv-Al8uWn7V7o7HhrMA9BvVWMGcA",
"terms" : [
{
"offset" : 0,
"value" : "UBCI Gremda"
},
{
"offset" : 13,
"value" : "C81"
},
{
"offset" : 18,
"value" : "Sfax"
},
{
"offset" : 24,
"value" : "Tunisia"
}
],
"types" : [ "establishment" ]
}
],
"status" : "OK"
}
有一个预测,并且在阵列适配器中正确得到这个但是当我输入“UBCI gr”时,自动完成并不会显示这个单个结果下拉。
答案 0 :(得分:0)
看起来“UBCI Gremda,C81,Sfax,突尼斯”是法语结果。您可以通过在网址中添加“&amp; language = fr”来使其正确显示。否则,Places API Web服务可能默认使用英语,但不包括该结果。
我还建议调查适用于Android的新Places API,这应该更简单一些。有关文档,请参阅https://developers.google.com/places/android/。
我希望这有帮助!