Google Places API PlaceFilter

2019-08-12 20:04发布

问题:

I am using Google Places API for Android and am trying to limit the results to only show Restaurants. I have included a PlaceFilter and seems to be correct but doesn't seem to be applying the filter and giving other results aside from Restaurants. See code:

ArrayList<String> restrictToRestaurants = new ArrayList<>();
                restrictToRestaurants.add(Integer.toString(Place.TYPE_RESTAURANT));
                PlaceFilter pf;
                pf = new PlaceFilter(false, restrictToRestaurants);

                PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, pf);
                result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
                    @Override
                    public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                        for (PlaceLikelihood placeLikelihood : likelyPlaces) {

                            placeLikelihood.getPlace().getPlaceTypes();

                            Log.i(TAG, String.format("Place '%s' with " + "likelihood: %g", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood()));
                        }
                        likelyPlaces.release();
                    }
                });

Anyone have any ideas how to apply the PlaceFilter?

回答1:

Currently, the only ways to filter the results of getCurrentPlace are outlined in the PlaceFilter class:

requireOpenNow if true, return only places open now

restrictToPlaceIds the specific PlaceIds to match

Each one of those PlaceIds is supposed to match to a specific location. Unfortunately, it looks like filtering the results of Places.PlaceDetectionApi.getCurrentPlace by the type of place (eg restaurants) is not supported yet. The Place.TYPE_RESTAURANT you referenced can be used in AutocompleteFilter for GeoDataApi.getAutocompletePredictions.