将PlaceFilter与PlaceDetection.getCurrentPlace一起使用

时间:2015-05-24 01:40:46

标签: android google-play-services google-places-api

我试图使用Google Play Services Places API以编程方式检索用户正在访问的餐馆。没有记录的方法根据当前API中的类别进行过滤,因此我使用Places Web服务调用来检索当前位置附近的餐馆。当用于在下面的循环中过滤结果时,PlaceFilter似乎正常运行,但在Places.PlaceDetectionApi.getCurrentPlace(mClient, filter)调用中使用时没有任何效果。有什么东西我不见了吗?

@Override
public Observable<Location> getCurrentLocation(String categories) {

    ArrayList<String> placeSet = new ArrayList<String>();
    NearbySearchResponse response = null;

    Observable<NearbySearchResponse> responseObservable = mGooglePlacesEndpoint.nearbySearchObservable(MAPS_API_KEY, "30.485277,-97.678878", 200, categories);

    // Perform a Places API query on the returned results of the filtered WS call

    return responseObservable
            .flatMap(RESPONSE_LOCATION_FUNC)
            .flatMap(LOCATION_ID_FUNC)
            .take(10) // API docs indicate a limit of 10 IDs
            .toList()
            .flatMap(new Func1<List<String>, Observable<Location>>() {

                @Override
                public Observable<Location> call(List<String> placeIds) {
                    final PlaceFilter filter = new PlaceFilter(true, placeIds);
                    return Observable.create(new Observable.OnSubscribe<Location>() {
                        @Override
                        public void call(final Subscriber<? super Location> subscriber) {
                            Log.d(TAG, "filter: " + StringUtils.join(filter.getPlaceIds(), "|"));
                            PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mClient, filter);

                            result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
                                @Override
                                public void onResult(PlaceLikelihoodBuffer placeLikelihoods) {
                                    for (PlaceLikelihood placeLikelihood : placeLikelihoods) {
                                        Place place = placeLikelihood.getPlace();
                                        if (!subscriber.isUnsubscribed() /*&& filter.matches(place)*/) // this filter.matches(place) is a work-around for a google bug
                                            subscriber.onNext(new GooglePlacesLocation(place, placeLikelihood.getLikelihood(), placeLikelihoods.getAttributions()));
                                    }
                                    Status status = placeLikelihoods.getStatus();
                                    if (!status.isSuccess()) {
                                        // @TODO propagate this error somehow
                                        // subscriber.onError();
                                    }
                                    placeLikelihoods.release();
                                    if (!subscriber.isUnsubscribed())
                                        subscriber.onCompleted();
                                }
                            });
                        }
                    });
                }
            });
}

0 个答案:

没有答案