Google 地理编码——解析可能返回不同结果的地址组件

发布于 2024-11-15 01:57:00 字数 1005 浏览 4 评论 0原文

我正在使用 Google 地图 V3 api。我正在提交地址搜索以返回正确的地理编码结果,包括地址、机构名称和纬度/经度。

我的问题是地理编码器的响应可以采用不同的格式。它始终遵循相同的结构,但某些响应对地址组件数据结构使用不同的键。

例如,某些搜索结果为:

establishment               -> location name
street_number               -> address street number
route                       -> the street name
locality                    -> the city
administrative_area_level_1 -> the state
postal_code                 -> zip/postal code

然而,如果我要搜索一般区域,例如“新罕布什尔州汉普顿海滩”,我会收到:

sublocality                 -> beach name / area
administrative_area_level_3 -> city/town name
administrative_area_level_1 -> the state
postal_code                 -> zip/postal code

如您所见,这两个响应有其差异。是否有一个已知的 jquery 库可用于处理这些不同的响应,以返回可用于人类可读方式的地址组件的数据集?

我会注意到,响应还返回一个“formatted_address”类型,它返回的结果如下:“Hampton Beach, NH 03842, USA”或“Boston University, 1 University Rd, Boston, MA 02215-1407, USA”如您所见,这些也有很大不同。我可以用逗号分隔,但我想使用实际的地址组件来实现完美的数据库插入。

I'm using Google Maps V3 api. I am submitting an address search to return the proper geocoded result including the address, name of establishment, and lat/lngs.

My problem is that the response from the geocoder can be formatted differently. It always follows the same structure, but some responses use different keys for the address_components data structure.

For Example, some searches result in:

establishment               -> location name
street_number               -> address street number
route                       -> the street name
locality                    -> the city
administrative_area_level_1 -> the state
postal_code                 -> zip/postal code

whereas, if i were to search a general area, such as "Hampton Beach, NH" i would receive:

sublocality                 -> beach name / area
administrative_area_level_3 -> city/town name
administrative_area_level_1 -> the state
postal_code                 -> zip/postal code

as you can see the two responses have their differences. Is there a known jquery library that can be used to handle these different responses to return a data set of the address components that can be used for a human-readable way?

I will note that the response also returns a "formatted_address" type, which returns it like: "Hampton Beach, NH 03842, USA" OR "Boston University, 1 University Rd, Boston, MA 02215-1407, USA" As you can see, these too are pretty different. I could split by comma, but I'd like to use the actual address_components for a flawless database insert.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三寸金莲 2024-11-22 01:57:00

为什么不让您的数据库仅镜像以下键?

street_number                    -> address street number
route                            -> the street name
locality                         -> the city/town    
administrative_area_level_3      -> the city/town
administrative_area_level_1      -> the state
postal_code                      -> zip/postal code

如果存在地点,请在您的请求中使用该地点(因为它似乎可以提供更详细的信息 - http://code.google.com/apis/maps/documentation/geocoding/#JSON )

没有街道名称的地方locality 存在,请求administrative_area_level_3administrative_area_level_1

当信息存在时,这将为您提供完整的人类可读的邮政地址,或者只是子区域(例如海滩)的城市/州,正如您提到的在其中一篇评论中。

**我假设你只关心美国。

Why not have your DB mirror only the following keys?

street_number                    -> address street number
route                            -> the street name
locality                         -> the city/town    
administrative_area_level_3      -> the city/town
administrative_area_level_1      -> the state
postal_code                      -> zip/postal code

Where a locality exists, use that in your request (as it appears to deliver the more detailed info - http://code.google.com/apis/maps/documentation/geocoding/#JSON )

Where no street name or locality exists, request administrative_area_level_3 and administrative_area_level_1

This would provide you with a full human-readable postal address when the info exists or just the city/state for a sublocality (e.g. beach), as you mentioned in one of the comments.

**I'm assuming you only care about the US.

铜锣湾横着走 2024-11-22 01:57:00

地址在很大程度上是一个“人类”的东西。我认为谷歌的数据之所以如此混乱,是因为他们的源数据很混乱,但在某个领域可能是相似的。

地址的哪些部分与您相关?如果你只是把这个喂给人类,就把你有的都给他吧?

Addresses are very much a 'human' thing. I think the reason Google's data is so messy is because their source data is messy, but likely similar for one area.

Which parts of the address are relevant to you? If you're just feeding this to a human just give him whatever you have?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文