gpt4 book ai didi

zipcode - 通过一次邮政编码搜索返回多个城市、州建议

转载 作者:行者123 更新时间:2023-12-02 12:03:26 26 4
gpt4 key购买 nike

我想使用某种 API 服务来获取邮政编码并返回与该邮政编码匹配的所有城市。

USPS 网站上的手动查找会执行此操作,而 Crate and Barrel 在其结账时会执行此操作。

USPS 的邮政编码 API 似乎只能返回一个城市。我查看了 Geonames 数据库,但它似乎也只为每个邮政编码的一个城市设置。

有办法实现这一点吗?

Crate and Barrel's checkout zipcode autocomplete

最佳答案

我将继续发布 @Matt 的答案,因为我已经使用了 SmartyStreets ZIP Code API就是为了这个目的。您可以通过以下三种方式之一查询此 API:

  1. 邮政编码
  2. 城市+州
  3. 城市 + 州 + 邮政编码

对于单个邮政编码(这就是您所询问的内容),这是一个简单的 GET 请求:

curl -v 'https://us-zipcode.api.smartystreets.com/lookup?
auth-id=YOUR+AUTH-ID+HERE&
auth-token=YOUR+AUTH-TOKEN+HERE&
zipcode=65202'

响应(具有有效的 AUTH-IDAUTH-TOKEN)是:

[
{
"input_index": 0,
"city_states": [
{
"city": "Columbia",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": true
},
{
"city": "Hinton",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Lindbergh",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Midway",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Murry",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Prathersville",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Shaw",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
},
{
"city": "Stephens",
"state_abbreviation": "MO",
"state": "Missouri",
"mailable_city": false
}
],
"zipcodes": [
{
"zipcode": "65202",
"zipcode_type": "S",
"default_city": "Columbia",
"county_fips": "29019",
"county_name": "Boone",
"state_abbreviation": "MO",
"state": "Missouri",
"latitude": 38.99775,
"longitude": -92.30798,
"precision": "Zip5",
"alternate_counties": [
{
"county_fips": "29027",
"county_name": "Callaway",
"state_abbreviation": "MO",
"state": "Missouri"
}
]
}
]
}
]

如您所见,这给出了与图片示例相同的城市列表。它还包含有关列表中第一个 city_state 的更多详细信息。

为了完整起见,下面是一个示例请求,演示了所有三个查询选项。 (注意:对于单个邮政编码之外的任何请求,API 需要 POST 调用):

curl -v 'https://us-zipcode.api.smartystreets.com/lookup?
auth-id=YOUR+AUTH-ID+HERE&
auth-token=YOUR+AUTH-TOKEN+HERE'
-H "Content-Type: application/json"
--data-binary '
[
{
"zipcode":"12345"
},
{
"city":"North Pole",
"state":"AK"
},
{
"city":"cupertino",
"state":"CA",
"zipcode":"95014"
}
]'

这是该示例请求的响应:

[
{
"input_index": 0,
"city_states": [
{
"city": "Schenectady",
"state_abbreviation": "NY",
"state": "New York",
"mailable_city": true
},
{
"city": "General Electric",
"state_abbreviation": "NY",
"state": "New York",
"mailable_city": false
},
{
"city": "Schdy",
"state_abbreviation": "NY",
"state": "New York",
"mailable_city": false
}
],
"zipcodes": [
{
"zipcode": "12345",
"zipcode_type": "U",
"default_city": "Schenectady",
"county_fips": "36093",
"county_name": "Schenectady",
"state_abbreviation": "NY",
"state": "New York",
"latitude": 42.81565,
"longitude": -73.94232,
"precision": "Zip5"
}
]
},
{
"input_index": 1,
"city_states": [
{
"city": "North Pole",
"state_abbreviation": "AK",
"state": "Alaska",
"mailable_city": true
}
],
"zipcodes": [
{
"zipcode": "99705",
"zipcode_type": "S",
"default_city": "North Pole",
"county_fips": "02090",
"county_name": "Fairbanks North Star",
"state_abbreviation": "AK",
"state": "Alaska",
"latitude": 64.77911,
"longitude": -147.36885,
"precision": "Zip5"
}
]
},
{
"input_index": 2,
"city_states": [
{
"city": "Cupertino",
"state_abbreviation": "CA",
"state": "California",
"mailable_city": true
}
],
"zipcodes": [
{
"zipcode": "95014",
"zipcode_type": "S",
"default_city": "Cupertino",
"county_fips": "06085",
"county_name": "Santa Clara",
"state_abbreviation": "CA",
"state": "California",
"latitude": 37.32056,
"longitude": -122.03865,
"precision": "Zip5"
}
]
}
]

希望对您有所帮助!

编辑 - 我已经用特定于该问题的示例更新了此内容。感谢@AmyAnuszewski 指出这一点。

关于zipcode - 通过一次邮政编码搜索返回多个城市、州建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24014750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com