按类型在Android中过滤Google地方信息结果

时间:2018-05-31 18:52:38

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

我在我的Android应用程序中使用Google Places API,尤其是PlaceAutocomplete。

我必须过滤结果才能显示学校。

我看到这个q&这似乎是我想要的,但它仅用于网络使用: Can Google Places API pull specific types of places, like schools, without a location?

我不知道如何对Android SDK做同样的事情......

我试试这个:

val typeFilter = AutocompleteFilter.Builder()
                    .setTypeFilter(Place.TYPE_SCHOOL)
                    .build()

            val intent = PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                    .setFilter(typeFilter)
                    .build(this)

            startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE)

但它不起作用

任何帮助???

谢谢, FAB

1 个答案:

答案 0 :(得分:3)

不幸的是,地方自动填充功能不支持过滤器中的详细类型。如果您查看文档,您将看到只有支持的类型

  • TYPE_FILTER_ADDRESS
  • TYPE_FILTER_CITIES
  • TYPE_FILTER_ESTABLISHMENT
  • TYPE_FILTER_GEOCODE
  • TYPE_FILTER_NONE
  • TYPE_FILTER_REGIONS

来源:https://developers.google.com/android/reference/com/google/android/gms/location/places/AutocompleteFilter

与Places API自动填充网络服务相同的情况:

https://developers.google.com/places/web-service/autocomplete#place_types

Google问题跟踪器中有一个非常旧的功能请求,用于添加详细类型过滤器到位自动完成功能。您可以在此处查看此功能请求:

https://issuetracker.google.com/issues/35820774

随意加注星标以添加投票并订阅通知。

如果您对实施类似于上述问题的Web服务调用感兴趣,请查看适用于Google Maps Services的Java客户端:

https://github.com/googlemaps/google-maps-services-java

使用此库,您可以在Java中实现文本搜索功能。像

这样的东西
GeoApiContext context = new GeoApiContext.Builder()
    .apiKey("AIza......")
    .build();

TextSearchRequest req = PlacesApi.textSearchQuery(context, "Springfield Elementary");
try {
    PlacesSearchResponse resp = req.type(PlaceType.SCHOOL).await();
    if (resp.results != null && resp.results.length > 0) {
        for (PlacesSearchResult r : resp.results) {
            //TODO: implement logic for places
        }
    }
} catch(Exception e) {
    Log.e(TAG, "Error getting places", e);
}

我希望这有帮助!