有人知道如何填充此自动填充功能,该用户只能使用城市名称吗?只有:伦敦,巴黎等,没有其他结果:“伦敦桥”,“伦敦眼”等 - 就像在例子中。 我在google places API Autocomplete中使用
所以我使用这个代码过滤器:
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
synchronized (filterResults) {
if (constraint != null) {
getAutocomplete(constraint);
result = mPlaceAPI.autocomplete(constraint.toString());
if (result != null) {
filterResults.values = result;
filterResults.count = result.size();
}
}
return filterResults;
并获得唯一的城市名称:
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("&types=(cities)");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
非常感谢:)
答案 0 :(得分:0)
我不知道你为什么要建立互联网请求,但如果没有必要,你可以使用谷歌Places API。 这是Googles guide
在您的应用中,您只需执行几个步骤:
1)构建并连接google API客户端
googleApiClient = new GoogleApiClient.Builder(Activity.this)
.addApi(Places.GEO_DATA_API)
.build();
googleApiClient.connect();
2)然后你几乎没有办法:使用自动完成意图,PlaceAutocompleteFragment或创建PendingResult<AutocompletePredictionBuffer>
,从中获取结果并在需要的地方使用它。我不得不使用最后一个,所以我创建了AutocompleteFilter
AutocompleteFilter.Builder builder = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES);
您可以看到AutocompleteFilter常量here 然后我创建了PendingResult
PendingResult<AutocompletePredictionBuffer> result
= Places
.GeoDataApi
.getAutocompletePredictions(googleApiClient, charSequence.toString(), null, builder.build());
并且在回调中做了我需要的事情
result.setResultCallback(new ResultCallback<AutocompletePredictionBuffer>{...});
在回调中,您可以通过以下方式接收信息:
Status status = autocompletePredictions.getStatus();
Handler handler = new Handler();
int size = autocompletePredictions.getCount();
if (status.isSuccess()){
for (int i = 0; i < size; i++){
//Do what you need
}
}
也许它不是最好的方式,但它适用于我,它类似于建立请求,就像你做的那样(在我看来)。无论如何,你可以在guide找到最适合你的方法。 希望我帮助你,如果我浪费你的时间,我很抱歉。
答案 1 :(得分:0)
这是我的代码我需要从结果autocomlete API google地方获取onlt city: MainActivity:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void searchPlace(final Editable editable) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!editable.toString().equals("") && mGoogleApiClient.isConnected()) {
try {
mAutoCompleteAdapter.getFilter().filter(editable.toString());
} catch (Exception e) {
Log.e(TAG, "Exception");
另一个活动:PlaceAPI
public 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("&types=(cities)");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
适配器:
private ArrayList<String> result;
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
synchronized (filterResults) {
if (constraint != null) {
getAutocomplete(constraint);
result = mPlaceAPI.autocomplete(constraint.toString());
if (result != null) {
filterResults.values = result;
filterResults.count = result.size();
}
}
return filterResults;
}
}
@Override
protected void publishResults(CharSequence constraint, final FilterResults filterResults) {
synchronized (filterResults) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (filterResults != null && filterResults.count >= 0) {
mResultList = resultList;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
});
}
}
};
return filter;
}
这是我添加到AutocompletePredictionBuffer:
private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) {
if (mGoogleApiClient.isConnected()) {
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);
final Status status = autocompletePredictions.getStatus();
if (!status.isSuccess()) {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
autocompletePredictions.release();
return new ArrayList<>();
}