@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.menu = menu;
MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.setIcon(ColorHelper.tintDrawable(
ResourcesCompat.getDrawable(getResources(), R.drawable.ic_search_black_24dp, null),
ContextCompat.getColor(this, R.color.colorPrimary))
);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
final CustomSearchView searchView = (CustomSearchView) MenuItemCompat.getActionView(menuItem);
searchView.setActivity(this);
CustomSearchView.SearchAutoComplete textArea = searchView.findViewById(R.id.search_src_text);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setSuggestionsAdapter(searchCursorAdapter);
searchView.setOnSuggestionListener(new CustomSearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
CursorAdapter ca = searchView.getSuggestionsAdapter();
Cursor cursor = ca.getCursor();
cursor.moveToPosition(position);
String placeId = cursor.getString(cursor.getColumnIndex(SearchCursorAdapter.SUGGESTION_ID));
if (placeId.equals("notPlaceId")) return true;
loadingDialog.show();
placesClient.fetchPlace(FetchPlaceRequest.newInstance(placeId,fields))
.addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {
@Override
public void onSuccess(FetchPlaceResponse fetchPlaceResponse) {
Log.i("PLACE: ",fetchPlaceResponse.getPlace().toString());
mapFragment.setFoundPlace(
fetchPlaceResponse.getPlace().getLatLng(),
fetchPlaceResponse.getPlace().getName(),
fetchPlaceResponse.getPlace().getId());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
// placesClient.getPlaceById(mGoogleApiClient, placeId)
// .setResultCallback(new ResultCallback<PlaceBuffer>() {
// @Override
// public void onResult(PlaceBuffer places) {
// loadingDialog.dismiss();
// if (places.getStatus().isSuccess() && places.getCount() > 0) {
// final Place myPlace = places.get(0);
// Log.i(TAG, "Place found: " + myPlace.getName());
// mapFragment.setFoundPlace(myPlace.getLatLng(), myPlace.getName(), myPlace.getId());
// } else {
// Log.e(TAG, "Place not found");
// }
// places.release();
// }
// });
return true;
}
@Override
public boolean onSuggestionSelect(int position) {
return true;
}
});
searchView.setOnQueryTextListener(new CustomSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
ViewHelper.hideSoftInput(MapsActivity.this);
return true;
}
@Override
public boolean onQueryTextChange(final String suggest) {
if ((suggest.length() > 1) && mGoogleApiClient.isConnected()) {
Log.i(TAG, "Starting autocomplete query for: " + suggest);
Cursor cursor = searchCursorAdapter.getCursor();
if ((cursor == null) || (cursor.moveToFirst() && (cursor.getInt(cursor.getColumnIndex(BaseColumns._ID)) == -1))) {
MatrixCursor matrixCursor = new MatrixCursor(new String[]{BaseColumns._ID, SearchCursorAdapter.SUGGESTION_ID});
matrixCursor.addRow(new Object[]{-2, getString(R.string.no_place_id)});
searchCursorAdapter.changeCursor(matrixCursor);
searchCursorAdapter.notifyDataSetChanged();
}
else {
AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
RectangularBounds bounds = RectangularBounds.newInstance(WORLD_BOUNDS);
final FindAutocompletePredictionsRequest request =
FindAutocompletePredictionsRequest
.builder()
.setCountry("KE")
.setLocationBias(bounds)
.setTypeFilter(TypeFilter.ADDRESS)
.setSessionToken(token)
.setQuery(suggest)
.build();
Log.i("REQUEST",request.toString());
placesClient.findAutocompletePredictions(request)
.addOnSuccessListener(new OnSuccessListener<FindAutocompletePredictionsResponse>() {
@Override
public void onSuccess(FindAutocompletePredictionsResponse findAutocompletePredictionsResponse) {
for (AutocompletePrediction prediction : findAutocompletePredictionsResponse.getAutocompletePredictions()) {
Log.i(TAG, prediction.getPlaceId());
Log.i(TAG, prediction.getPrimaryText(null).toString());
}
List<AutocompletePrediction> predictions = findAutocompletePredictionsResponse.getAutocompletePredictions();
MatrixCursor matrixCursor = new MatrixCursor(new String[]{BaseColumns._ID, SearchCursorAdapter.SUGGESTION_ID, SearchCursorAdapter.SUGGESTION_NAME, SearchCursorAdapter.SUGGESTION_EXTRA, SearchCursorAdapter.QUERY});
if (predictions.size() > 0) {
for (int i = 0; i < predictions.size(); i++) {
AutocompletePrediction prediction = predictions.get(i);
CharacterStyle style = new StyleSpan(Typeface.NORMAL);
Log.i(TAG, "Suggestion: " + prediction.getFullText(new StyleSpan(Typeface.NORMAL)));
matrixCursor.addRow(new Object[]{i, prediction.getPlaceId(), prediction.getPrimaryText(style), prediction.getSecondaryText(style), suggest});
}
}
else {
matrixCursor.addRow(new Object[]{-1, getString(R.string.no_place_id),
getString(R.string.primary_location_not_found, suggest),
getString(R.string.secondary_location_not_found), suggest});
}
searchCursorAdapter.changeCursor(matrixCursor);
searchCursorAdapter.notifyDataSetChanged();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
Log.e(TAG, "Place not found: " + apiException.getMessage());
}
}
});
}
} else {
searchCursorAdapter.changeCursor(null);
searchCursorAdapter.notifyDataSetChanged();
}
return true;
}
});
String fontPath = "fonts/" + getString(R.string.font_nunito)
+ "/" + getString(R.string.font_nunito) + "-Regular.ttf";
textArea.setTypeface(Typeface.createFromAsset(getAssets(), fontPath));
textArea.setHint(R.string.delivery_location);
textArea.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15.5f);
textArea.setCompoundDrawablesWithIntrinsicBounds(ColorHelper.tintDrawable(
ResourcesCompat.getDrawable(getResources(), R.drawable.ic_search_black_24dp, null),
ContextCompat.getColor(this, R.color.colorPrimary)), null, null, null);
textArea.setCompoundDrawablePadding(ViewHelper.dp2px(this, 10));
menuItem.expandActionView();
searchView.setIconified(false);
searchView.clearFocus();
return true;
}
我正在尝试使用Google places API获取附近地方的结果。我已经将API回调嵌入到"地图"活动顶部的搜索栏中。当我继续在搜索栏上键入时,建议应该会出现。建议将显示为一个列表,我可以从中选择一个地方,并使用该地方的坐标继续我的逻辑是否有更简单的方法来实现此功能?我的查询总是默认为API异常失败。
这是我的逻辑输出
2020-03-26 14:38:42.328 28308-28308/com.mobar.android.mobar I/MapsActivity: Starting autocomplete query for: Ruiru
2020-03-26 14:38:42.330 28308-28308/com.mobar.android.mobar I/REQUEST: FindAutocompletePredictionsRequest{query=Ruiru, locationBias=RectangularBounds{southwest=lat/lng: (-1.430571,36.650945), northeast=lat/lng: (-1.129622,37.120314)}, locationRestriction=null, origin=null, countries=[KE], sessionToken=3173b22c-afc6-4997-9f12-16690a6f7123, typeFilter=ADDRESS, cancellationToken=null}
2020-03-26 14:38:42.343 28308-30243/com.mobar.android.mobar I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-03-26 14:38:42.343 28308-30243/com.mobar.android.mobar I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-03-26 14:38:42.347 7025-7025/? I/SKBD: [ATIM] [updateSelectionForInputModule] oldSelStart : 4, oldSelEnd : 4, newSelStart : 5, newSelEnd : 5, candidatesStart : 0, candidatesEnd : 5, viewClicked : false
2020-03-26 14:38:42.348 7025-7025/? I/SKBD: [ATIM] [updateSelectionForInputModule] isTyping : true, viewClicked : false, isKBDShown : true
2020-03-26 14:38:42.367 28308-28308/com.mobar.android.mobar E/MapsActivity: Place not found: 9011: The provided API key is invalid.
错误表明API密钥无效,但事实并非如此,因为当我使用API密钥执行Web查询时,我会得到结果。
您得到的错误表明问题出在您的API密钥上。首先,仔细检查您是否正确添加了API密钥,密钥两端没有任何空格或额外字符。
第二,确保它受到适当的限制;它需要被安卓限制为你的应用程序的包名和指纹。如果您还添加了API限制,请检查Places API是否已列出。出于测试目的,您可能还想暂时完全取消限制,然后测试Place Autocomplete是否使用不受限制的密钥。
希望这对你有帮助!