Google Places API Android根据地点类型进行过滤

时间:2017-01-03 08:50:38

标签: android google-maps google-places-api

我正在尝试过滤附近的地方,特别是医院。我正在使用适用于Android的Google Places API。我的下面的代码没有任何错误,但它没有显示任何医院。

List<String> filters = new ArrayList<>();
filters.add(String.valueOf(Place.TYPE_HOSPITAL));
PlaceFilter placeFilter = new PlaceFilter(false, filters);

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

                    mMap.addMarker(new MarkerOptions()
                            .position(placeLikelihood.getPlace().getLatLng())
                            .title((String) placeLikelihood.getPlace().getName())
                            .snippet((String) placeLikelihood.getPlace().getPhoneNumber()));
                }
                likelyPlaces.release();
            }
        });

2 个答案:

答案 0 :(得分:3)

您可以使用placeLikelihood.getPlace().getPlaceTypes()然后使用for循环并搜索特定的类别编号 https://developers.google.com/android/reference/com/google/android/gms/location/places/Place.html?hl=es-419#TYPE_RESTAURANT

答案 1 :(得分:0)

我同意以上答案。这是一个带回“ RESTAURANT”类型的位置的循环示例。它使用[https://developers.google.com/places/android-sdk/place-details#get-place]

中描述的位置似然方法
//Looping through each placeLikelihood objects.
for(PlaceLikelihood placelikelihood : response.getPlaceLikelihoods){
    //Convert the place types obtaines from Place Likelihood into an array so that we can loop through
    Object[] typeOfPlace = placeLikelihood.getPlace().getTypes().toArray();
    for(Object placeType : typeOfPlace){
       if(placeType.toString().equals("RESTAURANT")){
           Timber.i("Place is %s", placeLikelihood.getPlace().getName);
       }        
    }   
}

考虑到这一点,我认为这对您会有所帮助。

for(PlaceLikelihood placeLikelihood : likelyPlaces){
    Object[] typeOfPlace = placeLikelihood.getPlace().getTypes().toArray();
    for(Object placeType : typeOfPlace){
            if(placeType.toString().equals("HOSPITAL")){
                mMap.addMarker(new MarkerOptions()
                        .position(placeLikelihood.getPlace().getLatLng())
                        .title((String) placeLikelihood.getPlace().getName())
                        .snippet((String) 
                         placeLikelihood.getPlace().getPhoneNumber()));

            }
    }

}