我正在使用 Android Google Places API 自动补全街道和地址。问题是它给出了整个国家的所有街道。当然,我添加了限制搜索位置的界限,但它不能正常工作 - 它只给出优先级,所以换句话说,最好的结果将在列表中更高,仅此而已
所以代码:
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())
中设置的范围。 。想象一下,几乎每个地方都存在类似“列宁街”的东西!
如何解决问题并限制搜索结果?
附注
我知道我可以使用Google Places Web API
或通过GeoDataApi.getAutocompletePredictions()
并手动过滤结果, 但这意味着我也必须手动编写 UI,这是我不想做的 做。 罢工> 这比我想象的还要糟糕。即使我从 Web API 或通过 GeoDataApi 获得结果,我也只有不包含坐标的预测,只有 placeId。因此,如果我想按坐标过滤预测,我必须对 each placeId 进行请求。换句话说,如果我得到 20 个位置,我将不得不再执行 20 个请求才能找到坐标。- 我还可以在
searchString
中添加城市名称,这使得结果更好(但根本不是),但它使地址书写不清楚并且出现城市名称,所以它也不是一个好的解决方案。
最佳答案
恐怕 Android 版 Places API 尚不支持严格限制。 Google 问题跟踪器中有一个功能请求来实现此目的:
https://issuetracker.google.com/issues/38188994
欢迎为此功能请求加注星标,以投票并订阅来自 Google 的通知。
同时,解决方法可能是使用支持 strict bounds 的 Places API Web 服务并手动实现 UI。
更新
该功能请求已被 Google 标记为已修复。看看https://stackoverflow.com/a/50134855/5140781显示如何在 Android 版 Places API 中应用严格边界。
关于java - 限制 Android Google Places API 仅搜索给定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46522443/