邮政编码查询
输入邮政编码,返回它所属的地点。覆盖 160 个国家。
GEThttps://api.mygeocode.com/v1/postcode
参数
| 参数 | 类型 | 说明 |
|---|---|---|
code必填 | 字符串 | 邮政编码。空格、连字符和大小写会被规范化。 |
country可选 | 字符串 | ISO 3166-1 alpha-2 代码。强烈建议填写;邮政编码在不同国家之间并不唯一。不填写时,会返回所有匹配国家的结果,人口最多的排在前面。 |
lang可选 | 字符串 | 地点和地区名称所用的 ISO 639-1 代码。默认为 en。 |
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 8e Arrondissement",
"region": "Ile-de-France",
"region_code": "IDF",
"country": "France",
"country_code": "FR",
"lat": 48.8727,
"lon": 2.3125
}
]
}响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
postcode | 字符串 | 规范化后的编码。 |
country_code | 字符串或 null | 所应用的国家筛选条件(如有)。 |
results | 数组 | 该编码覆盖的地点。通常为一个;跨越多个村庄的乡村编码会有多个。编码未知时为空。 |
results[].place | 字符串 | 邮政机构使用的地名。 |
results[].region, region_code | 字符串 | 一级行政区及其 ISO 3166-2 后缀。 |
results[].lat, lon | 数字 | 邮政机构公布的坐标点(如有),否则为该编码内地址的质心。 |
注意事项
- 在国家编码体系为层级式的地方,部分编码也可使用:英国的
SW1A会返回对应区域;法国的750则不返回任何结果,因为法国的编码不是前缀式的。 - 没有邮政编码的国家(Eircode 之前的爱尔兰,以及非洲和中东的一些国家)会返回空的
results数组和status: ok。 - 对于带门牌号的完整地址,请使用正向地理编码;邮政编码对它也有帮助。
错误
当 code 为空或超过 16 个字符,或 country 不是有效的 alpha-2 代码时,返回 400 invalid_request。