邮政编码.io 客户端
摘要访问 postcodes.io API (英语).
var PostcodesIO = require('postcodesio-client');
var postcodes = new PostcodesIO();
postcodes.lookup('EC1V 9LB').then(postcode => {
console.log(postcode);
// {
// "postcode": "EC1V 9LB",
// "admin_district": "Islington",
// "longitude": -0.091247681768113,
// "latitude": 51.5278436902703,
// "region": "London",
// ...
// }
});
安装
$ npm install postcodesio-client
使用量
创建客户端实例, 如果您愿意提供选项 :
var PostcodesIO = require('postcodesio-client');
var postcodes = new PostcodesIO('https://api.postcodes.io', {
headers: { 'User-Agent': 'MyAwesomeApp/1.0.0' } // default {} - extra headers
});
使用承诺提出请求:
postcodes
.lookup('EC1V 9LB')
.then(function (postcode) {
console.log(postcode);
}, function (error) {
console.log('oh no, it failed ;_;');
});
未发现API的答复(404个)不被认为是错误. 相反
输出会是 null。 。 。 .
postcodes.lookup('F4K3AA').then(postcode => {
console.log(postcode);
// null
});
只有承诺(承诺/A+支持 - 回调 没有。 以下两节具体说明如何实施承诺 参见下文.
承诺
postcodes
.method(parameters...)
.then(function (outputs...) {
}, function (error) {
});
method- 你要使用的方法 - 什么parameters- 该方法特有的任何论据(见 方法(中文(简体) )error- 实例Erroroutputs- 该方法的任何产出(见 方法(中文(简体) )
两者 parameters 财务报告和审定财务报表 outputs 可以指: 零,一,或许多论据. 这些是
为每种方法指定.
方法
查询
获取大量数据用于邮编码或出码.
postcodes.lookup(code)
参数:
code- 字符串 - 要取回的邮编码或出码
产出:
这也可被明确称为: postcodes.lookupPostcode(postcode) 或者说 postcodes.lookupOutcode(outcode)。 。 。 .
近坐标
查找最接近坐标的邮政编码.
postcodes.near(latitude, longitude)
这也可被明确称为: postcodes.nearCoordinate(latitude, longitude, options)。 。 。 .
参数:
latitude坐标的纬度longitude坐标的经度options- 对象 - 可选的,只有明确调用时可用
产出:
postcodes- 数组 邮政编码 - 附近的邮政编码
贴近邮政编码
查找最接近另一个邮编的邮编.
警告:结果可能包括原邮编码.
postcodes.near(postcode)
这也可被明确称为: postcodes.nearPostcode(postcode)。 。 。 .
参数:
postcode字符串 - 要搜索的邮编
产出:
postcodes- 数组 邮政编码 - 附近的邮政编码
反转地理代码
找到最接近坐标的单一邮编 .
postcodes.reverseGeocode(latitude, longitude)
参数:
latitude坐标的纬度longitude坐标的经度
产出:
postcode- 说吧 邮政编码 - 附近的邮编
校验
验证邮编是否存在 。 (意思是它在官方的皇家邮件中) 邮政编码地址文件).
postcodes.validate(postcode)
参数:
postcode字符串 - 要检索的邮编
产出:
exists- 布尔 - 表示邮政编码的存在
随机
获取随机邮编 。 不太确定你为什么要这么做 但这里 这是...
postcodes.random()
产出:
postcode- 说吧 邮政编码 - 随机邮政编码
类型
邮政编码
示例
{
"postcode": "EC1V 9LB",
"quality": 1,
"eastings": 532506,
"northings": 182719,
"country": "England",
"nhs_ha": "London",
"admin_county": null,
"admin_district": "Islington",
"admin_ward": "Bunhill",
"longitude": -0.091247681768113,
"latitude": 51.5278436902703,
"parliamentary_constituency": "Islington South and Finsbury",
"european_electoral_region": "London",
"primary_care_trust": "Islington",
"region": "London",
"parish": null,
"lsoa": "Islington 023A",
"msoa": "Islington 023",
"nuts": null,
"incode": "9LB",
"outcode": "EC1V",
"ccg": "NHS Islington"
}
代码
示例
{
"outcode": "EC1V",
"longitude": -0.0981811622126924,
"latitude": 51.5266761246198,
"northings": 182576,
"eastings": 532028,
"admin_district": [
"Hackney",
"Islington"
],
"parish": [
"Hackney, unparished area",
"Islington, unparished area"
],
"admin_county": [],
"admin_ward": [
"Bunhill",
"Hoxton West",
"Clerkenwell",
"Hoxton East & Shoreditch",
"St Peter's"
],
"country": [
"England"
]
}
测试
首先安装开发依赖 :
$ npm install
然后进行测试:
$ npm test
支助
请打开此寄存器 .
作者数
- 詹姆斯·比林汉姆 干扰@ jamesbillingham.com
许可证
麻省理工学院执照 - 看 自由主义 文件