使用 Google PLACES Api 搜索查看自动完成建议

Posted

技术标签:

【中文标题】使用 Google PLACES Api 搜索查看自动完成建议【英文标题】:SearchView Autocomplete suggestions using Google PLACES Api 【发布时间】:2020-07-07 01:52:51 【问题描述】:
@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: FindAutocompletePredictionsRequestquery=Ruiru, locationBias=RectangularBoundssouthwest=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 Key 无效,这不是真的,因为当我使用我的 API Key 执行 web 查询时,我得到了结果。

【问题讨论】:

【参考方案1】:

您收到的错误表明问题出在您的 API 密钥上。首先,仔细检查您的 adding your API key 是否正确,键的两端没有任何空格或多余的字符。

其次,确保它受到适当的限制;您的应用程序包名称和指纹必须是 Android restricted。如果您还添加了 API 限制,请检查是否列出了 Places API。出于测试目的,您可能还想暂时完全取消限制,然后测试“地点自动完成”是否适用于不受限制的键。

希望对您有所帮助!

【讨论】:

以上是关于使用 Google PLACES Api 搜索查看自动完成建议的主要内容,如果未能解决你的问题,请参考以下文章

使用Javascript从Google Places搜索api获取纬度和经度

Google Places API 结果与 Google 搜索不匹配

如何使用 Google Places Javascript API 搜索附近(自动从浏览器获取坐标)?

如何使用 Google Places Autocomplete API iOS 保存以前搜索过的结果

Google Places API - 附近搜索不如地图准确?

Google Places api 无法自动完成新 api 库中的搜索查询