PlaceFilter Place Type from Google Places API Andr

2019-06-08 03:42发布

问题:

I want to ask how to get nearby Places with filter type (Example: shopping mall, bank) from new google places api (https://developers.google.com/places/android/start)

Here my code (I already add some alternative in loop conditions), but I want to filter types from PlaceFilter not in loop conditions.

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
            .getCurrentPlace(mGoogleApiClient, null);
    result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
        @Override
        public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
            for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                list = placeLikelihood.getPlace().getPlaceTypes();
                for(int i = 0; i < list.size(); i++)
                {
                    if(list.get(i) == Place.TYPE_SHOPPING_MALL) {
                        Log.i(TAG, String.format("Place '%s' has likelihood: %g",
                                placeLikelihood.getPlace().getName(),
                                placeLikelihood.getLikelihood()));
                    }
                }
            }
            likelyPlaces.release();
        }
    });

Please help, Many thanks

回答1:

I think you are trying to do something like this.

     List<Integer> filters=new ArrayList<>();
     filters.add(Place.TYPE_ESTABLISHMENT); 
     AutocompleteFilter autocompleteFilter=AutocompleteFilter.create(filters);
     PendingResult<AutocompletePredictionBuffer> pendingResult=Places
         .GeoDataApi
         .getAutocompletePredictions(sGoogleApiClient,"delhi", rectangleLyon, autocompleteFilter);
 //rectangleLyon is LatLngBounds, to remove filters put autocompletefilter as null 
 // Second parameter(as String "delhi") is your search query
     AutocompletePredictionBuffer autocompletePredictionBuffer=pendingResult.await(10, TimeUnit.SECONDS);
     Status status=autocompletePredictionBuffer.getStatus();
     Iterator<AutocompletePrediction> iterator=autocompletePredictionBuffer.iterator();

        while (iterator.hasNext()){
            AutocompletePrediction autocompletePrediction=iterator.next();
            // do something
        }          

You can add more filters using

filters.add(Place.<MORE FILTERS>); //example TYPE_AIRPORT

Check here for more filter types https://developers.google.com/places/supported_types