限制Android Google Places API仅搜索给定区域 [英] Limit Android Google Places API to search only given area

查看:86
本文介绍了限制Android Google Places API仅搜索给定区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Google Places API自动完成街道和地址.问题在于,它覆盖了整个国家的所有街道.当然,我添加了边界来限制搜索的位置,但是它无法正常工作-它仅给出优先级,因此换句话说,最佳结果在列表中会更高,仅此而已

I`m using Android Google Places API to autocomplete streets and addresses. The problem is that it gives all streets from a whole country. Of course I added bounds to limit place for search, but it doesnt work correctly - it gives only priority, so in other words best results will be higher in list, nothing more

所以代码:

       AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
                .setCountry("RU")
                .build();

        Intent intent =
                new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                        .zzih(searchString) //that is for passing search string from toolbar
                        .setFilter(typeFilter)
                        .setBoundsBias(city.getBounds())
                        .build(this);

简而言之,问题是: 当我输入"Lenina Street"之类的搜索内容时,我看到了很多无用的结果,这些结果超出了.setBoundsBias(city.getBounds())中设置的范围.试想一下,几乎每个地方都存在类似"Lenina Street"的东西!

In short the problem is: When I type in search something like "Lenina Street" I see a lot of useless results out of bounds set in .setBoundsBias(city.getBounds()). Just imagine that something like "Lenina Street" exists in almost every locality!

如何解决该问题并限制搜索结果?

How can I fix the problem and limit search results?

PS

  1. 我知道我可以使用Google Places Web APIGeoDataApi.getAutocompletePredictions()并手动过滤结果, 但这意味着我也必须手动编写UI,我不想 做. 那比我想象的还要糟糕.即使我从Web API或通过GeoDataApi获得结果,我也只有不包含坐标的预测,只有placeId.因此,如果我想按坐标过滤预测,则必须请求每个 placeId.换句话说,如果我有20个位置,我将不得不再执行20个请求才能找到坐标.
  2. 我还可以在searchString中添加城市名称,这样可以使结果更好(但一点也不好),但是这使得地址的写法不清楚并且发生了城市名称,因此它也不是很好的解决方案.
  1. I know I can use Google Places Web API or by GeoDataApi.getAutocompletePredictions() and filter results manually, but that means I have to write UI manually too, what I dont want to do. Thats even worse than I thought. Even if I get results from Web API or through GeoDataApi I have only predictions which doesnt contain coordinates, only placeId. So if I want to filter predictions by coordinates I have to do request for each placeId. In other words if I got 20 places I will have to do 20 more requests to find out coordinates.
  2. Also I can add city name in searchString, that makes results better (but not at all) but it makes writing of address unclear and city name takes place, so its not good solution too.

推荐答案

恐怕Android版Places API尚不支持严格限制. Google问题跟踪器中有一项功能要求,可以实现此功能:

I'm afraid Places API for Android doesn't support strict bounds yet. There is a feature request in Google Issue tracker to implement this:

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

随时为该功能请求加注星标,以添加您的投票并订阅来自Google的通知.

Feel free to star this feature request to add your vote and subscribe to notifications from Google.

同时,解决方法可能是使用支持严格边界并手动实现用户界面.

In the meantime the workaround might be using Places API web service that supports strict bounds and implement the UI manually.

更新

功能请求已被Google标记为已修复".看看 https://stackoverflow.com/a/50134855/5140781 ,其中显示了如何在位置中应用严格限制适用于Android的API.

The feature request was marked as Fixed by Google. Have a look at https://stackoverflow.com/a/50134855/5140781 that shows how to apply strict bounds in Places API for Android.

这篇关于限制Android Google Places API仅搜索给定区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆