Google地理编码 - 解析可能以不同方式返回的address_components

Att*_*cus 9 javascript google-maps geocoding google-geocoder

我正在使用Google Maps V3 api.我正在提交地址搜索以返回正确的地理编码结果,包括地址,建立名称和lat/lngs.

我的问题是来自地理编码器的响应可以采用不同的格式.它始终遵循相同的结构,但是一些响应对address_components数据结构使用不同的键.

例如,某些搜索会导致:

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
Run Code Online (Sandbox Code Playgroud)

然而,如果我要搜索一般区域,例如"Hampton Beach,NH",我会收到:

sublocality                 -> beach name / area
administrative_area_level_3 -> city/town name
administrative_area_level_1 -> the state
postal_code                 -> zip/postal code
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,这两个回答有其不同之处.是否有一个已知的jquery库可用于处理这些不同的响应,以返回可用于人类可读方式的地址组件的数据集?

我会注意到响应也返回一个"formatted_address"类型,它返回它:"Hampton Beach,NH 03842,USA"或"Boston University,1 University Rd,Boston,MA 02215-1407,USA"正如你所看到的,这些也是非常不同的.我可以用逗号分割,但我想使用实际的address_components来实现完美的数据库插入.

Jas*_*aro 14

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

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
Run Code Online (Sandbox Code Playgroud)

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

如果没有street namelocality存在,请求administrative_area_level_3administrative_area_level_1

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

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