我正在使用 Google map 的地理编码 API 来获取曼哈顿此地址的 lat、lng 值:
123 Dr Martin Luther King Jr Blvd,纽约,NY 10035,美国
我的初始查询如下所示:https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd
此查询不会返回我想要的地址,因此为了将结果过滤为仅位于曼哈顿的地址,我根据请求进行了修改,以包含组件过滤参数locality:Manhattan
。
如文档中所定义:https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
所以我的问题是 - 为什么这个网址是:https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd&components=locality:Manhattan没有返回我想要的地址?
然而 - 使用组件过滤器 postal_code:10035
确实返回了我想要的地址:https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd&components=postal_code:10035
谢谢
最佳答案
我认为您应该了解的主要细节是 Google 最近更改了组件过滤器的行为。此更改于 2018 年 1 月 15 日进行。文档现在包含以下文本:
Filters on route, locality, and administrative_area influence the results but do not guarantee containment in the same way as an address query.
来源:https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
这意味着目前仅对组件过滤器严格执行邮政编码和国家/地区。地理编码 API 中的组件过滤不再像以前那么严格。此更改解释了示例请求的行为。具有位置的组件过滤器不严格,因此您会得到不同的结果,而具有邮政编码的组件过滤器很严格,您会得到预期的结果。
如果您需要地点,则应将其包含在请求的 address
参数中,以使其不那么模糊。
例如
https://maps.googleapis.com/maps/api/geocode/json?address=123%20mlk%20blvd%20Manhattan&key=YOUR_API_KEY
您应该遵循地理编码 API 的最佳实践,这些实践位于:
https://developers.google.com/maps/faq#geocoder_queryformat https://developers.google.com/maps/documentation/geocoding/best-practices
希望我的回答能解答您的疑惑!
关于google-maps - 使用字段 "locality"进行地理编码组件过滤未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48346019/