邮政编码查询
输入邮政编码,返回它所属的地点。覆盖 121 个国家。
GEThttps://api.mygeocode.com/v1/postcode
参数
| 参数 | 类型 | 说明 |
|---|---|---|
code必填 | 字符串 | 邮政编码。空格、连字符和大小写会被规范化。 |
country可选 | 字符串 | ISO 3166-1 alpha-2 代码。强烈建议填写;邮政编码在不同国家之间并不唯一。不填写时,会返回所有匹配国家的结果,人口最多的排在前面。 |
key可选 | 字符串 | API 密钥(如果未通过请求头发送)。 |
示例
$ curl -H "X-API-Key: YOUR_KEY" "https://api.mygeocode.com/v1/postcode?code=75008&country=fr"const url = new URL("https://api.mygeocode.com/v1/postcode");
url.searchParams.set("code", "75008");
url.searchParams.set("country", "fr");
const { results } = await (await fetch(url, { headers: { "X-API-Key": "YOUR_KEY" } })).json();
console.log(results[0].place, results[0].lat, results[0].lon);import requests
r = requests.get("https://api.mygeocode.com/v1/postcode", params={"code": "75008", "country": "fr"}, headers={"X-API-Key": "YOUR_KEY"}, timeout=5)
first = r.json()["results"][0]
print(first["place"], first["lat"], first["lon"])响应
{
"status": "ok",
"postcode": "75008",
"country_code": "FR",
"results": [
{
"postcode": "75008",
"place": "Paris",
"region": "Île-de-France",
"region_code": null,
"country": "France",
"country_code": "FR",
"lat": 48.853402,
"lon": 2.348799
},
{
"postcode": "75008",
"place": "Paris 08",
"region": "Île-de-France",
"region_code": null,
"country": "France",
"country_code": "FR",
"lat": 48.876299,
"lon": 2.318299
}
]
}响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
postcode | 字符串 | 规范化后的编码。 |
country_code | 字符串或 null | 所应用的国家筛选条件(如有)。 |
results | 数组 | 该编码覆盖的地点。通常为一个;跨越多个村庄的乡村编码会有多个。编码未知时为空。 |
results[].postcode | 字符串 | 按该国书写方式给出的邮政编码:K1A 0B1。 |
results[].place | 字符串 | 邮政机构使用的地名。 |
results[].region, region_code | 字符串 | 一级行政区及其 ISO 3166-2 后缀(没有一级行政区时 region_code 为 null)。 |
results[].country, country_code | 字符串 | 国家名称和大写的 ISO 3166-1 alpha-2 代码。 |
results[].lat, lon | 数字 | 邮政机构公布的坐标点(如有),否则为该编码内地址的质心。 |
注意事项
- 部分编码会返回所有以其开头的编码的区域:英国的
SW1A返回邮政编码区(postcode district),加拿大的K1A返回前向分拣区(forward sortation area),设置了country时,任何国家编码的开头部分(法国为750)返回其编码覆盖的区域。 - 没有邮政编码的国家(Eircode 之前的爱尔兰,以及非洲和中东的一些国家)会返回空的
results数组和status: ok。 - 加拿大支持完整的六位邮政编码(
K1A 0B1)以及前三位(K1A),加拿大地址也带有完整的邮政编码。 - 对于带门牌号的完整地址,请使用正向地理编码;邮政编码对它也有帮助。
错误
当 code 为空或超过 16 个字符,或 country 不是有效的 alpha-2 代码时,返回 400 invalid_request。