google-maps - Google Place Autocomplete API 显示无效地址

标签 google-maps autocomplete google-places-api google-places

我们目前正在尝试 Google places autocomplete api , 但似乎谷歌放置了 autocomplete api还会在搜索结果中返回无效(不存在)地址。

当用户输入地址自动完成时,我们希望只接收有效/实际地址,有什么办法吗?

我搜索了 api文档,但找不到任何有用的东西。 有一个 google places validation api但它只在给定一个地址后才进行验证,但我们只想在搜索自动完成时显示有效/实际地址,而且不想调用验证 api对于每个搜索结果项

浏览了官方文档并尝试了几个 stackoverflow 答案,但这些答案不符合我们的要求。

SO ref-1

最佳答案

目前一切正常

我尝试使用这个重现自动完成请求:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=332%20Kennedy%20drive,%20Torrington,%20CT&types=address&key=API_KEY

并且返回的结果有一个很长的Place ID

根据 Places API documentation :

"Place IDs uniquely identify a place in the Google Places database and on Google Maps."

documentation还表示,API 可能会在响应中返回不同的地点 ID。这些地点 ID 类型包括:

  • Street addresses that do not exist in Google Maps as precise addresses, but are inferred from a range of addresses.
  • Segments of a long route, where the request also specifies a city or locality.
  • Intersections.
  • Places with an address component of type subpremise.

这些 ID 通常采用长字符串的形式。

这解释了为什么 API 仍然使用 types=address 为输入 =332 Kennedy drive, Torrington, CT 返回街道 address。自动完成的用例是帮助用户获得最接近的结果,即使他们请求的查询不明确。所以自动完成总是试图返回最接近输入和类型的结果。在您的例子中,它返回了一个街道地址,这些地址在 Google map 中并不作为精确地址存在,而是从一系列地址中推断出来的

有一个 public bug在与此相关的问题跟踪器上。在评论 #5 中,它说:

Suggestions are sometimes created for addresses that may not exist, if there are chances that they do. Retrieving details for such suggestions may result in the suggested address, if it actually exists or can be synthesized from existing data. Otherwise, the result may be a different address, one that actually exists.

可能的解决方法

您可以做的一件事是使用验证 API。

但是既然你提到你不想使用它,我尝试使用这个自动完成请求:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=332%20Kennedy%20drive,%20Torrington,%20CT&types=establishment&key=API_KEY

我刚刚将 types=address 更改为 types=establishment,它似乎返回了合法地址。我不确定这是否适合您的用例,所以如果不适合,您可以发表评论。

如果没有,我在问题跟踪器上发现了一个类似的公共(public)错误,它表示谷歌工程师目前正在内部处理类似的问题。所以我建议您等待此 public bug 的更新.

没有关于修复多长时间的明确时间表,但您可以为其加注星标,以便在有更新时收到通知。

希望这对您有所帮助!

==更新==

我尝试使用这些类型并尝试从该表中放入一个类型数组。我试过这个:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=332%20Kennedy%20drive,%20Torrington,%20CT&types=street_address|street_number|premise&key=API_KEY

它确实返回了一些真实地址。我将 types=address 更改为 types=street_address|street_number|premise。您可以自己尝试一下,尝试一下这些类型,看看它是否适用于您的用例。

有关类型的更多信息,这里是link to the documentation .

关于google-maps - Google Place Autocomplete API 显示无效地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74766805/

相关文章:

google-maps - Google 地理编码器如何工作?

c++ - 如何更改QCompleter的弹出列表的位置?

javascript - 自动完成 jquery 多个字段 onchange 或 tab 事件

ios - Google Places API 违规

javascript - 如何将 Javascript 函数返回的值传递给 django 中的 url?

r - 在 Google Maps API 中制作多个样式引用

mysql - 尝试保存小数时始终保存 999.999999

java - Google 将 API 返回单一类型 "car_repair"

android - Google Maps API 版本差异

solr - 如何在solr中实现自动建议?