nodejs geocoding library
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 2 年前 | ||
| 3 年前 | ||
| 10 个月前 | ||
| 1 年前 | ||
| 6 年前 | ||
| 4 年前 | ||
| 4 年前 | ||
| 6 年前 | ||
| 1 年前 | ||
| 11 年前 | ||
| 12 年前 | ||
| 2 年前 | ||
| 4 年前 | ||
| 1 年前 |
以下内容由 AI 翻译,如有问题请 点此提交 issue 反馈
Node-Geocoder
这是一个用于地理解码(地址到经纬度)和逆地理编码(经纬度到地址)的 Node.js 库,适用于作为 Node.js 的库使用。
安装(Node.js 库)
npm install node-geocoder
使用示例
const NodeGeocoder = require('node-geocoder');
const options = {
provider: 'google',
// 取决于提供商的可选配置
fetch: 自定义Fetch实现,
apiKey: 'YOUR_API_KEY', // 对于Mapquest、OpenCage、APlace、Google Premier适用
formatter: null // 'gpx', 'string' 等...
};
const geocoder = NodeGeocoder(options);
// 异步回调方式使用
const result = await geocoder.geocode('29 champs elysée paris');
// 输出结果示例:
[
{
latitude: 48.8698679,
longitude: 2.3072976,
country: '法国',
countryCode: 'FR',
city: '巴黎',
zipcode: '75008',
streetName: '香榭丽舍大街',
streetNumber: '29',
administrativeLevels: {
level1long: '法兰西岛',
level1short: 'IDF',
level2long: '巴黎',
level2short: '75'
},
provider: 'google'
}
];
高级用法(仅限谷歌、Here、MapQuest、LocationIQ和OpenCage提供者)
const result = await geocoder.geocode({
address: '29 champs elysée',
country: 'France',
zipcode: '75008'
});
// OpenCage高级用法示例
const result = await geocoder.geocode({
address: '29 champs elysée',
countryCode: 'fr',
minConfidence: 0.5,
limit: 5
});
// 逆向地理编码示例
const result = await geocoder.reverse({ lat: 45.767, lon: 4.833 });
// 批量地理编码
const results = await geocoder.batchGeocode([
'13 rue sainte catherine',
'另一个地址'
]);
// 设置特定HTTP请求头
const nodeFetch = require('node-fetch');
const geocoder = NodeGeocoder({
provider: 'google',
fetch: function fetch(url, options) {
return nodeFetch(url, {
...options,
headers: {
'user-agent': '我的应用 <email@domain.com>',
'X-Specific-Header': '具体值'
}
});
}
});
地理编码服务提供者(按字母排序)
- agol:ArcGIS Online 地理解码服务,支持正反向解码,需要client_id和client_secret。
- aplace:APlace.io地理解码服务,也支持正反向解码,需设置访问令牌。
- datasciencetoolkit:DataScienceToolkit 地理解码器,支持IPv4和地址解码。
- freegeoip:FreeGeoIP IP地理解码。
- geocodio:美国地区的地址和反向地理解码。
- google:Google 地理解码器,支持正反向解码。商业许可需要
clientId和apiKey。 - here:HERE 地理解码器,同样支持正反向解码,并允许指定API密钥及语言、国家代码和区域。
- locationiq:类似OpenStreetMap,但需设置LocationIQ API密钥。
- mapbox:MapBox 地理解码,需要apiKey。
- mapquest:MapQuest 地理解码,需apiKey。
- nominatimmapquest:与OpenStreetMap相似的MapQuest服务器查询。
- opencage:整合多种开源地解服务,支持地址和反向解析,需要获取API密钥。
- opendatafrance:专注法国的正反向地理解码。
- openmapquest:基于OpenStreetMap的地理解码器,需apiKey。
- openstreetmap:原生OpenStreetMap地理解码,支持自定义语言和邮箱。
- pickpoint:PickPoint 地理解码服务,支持正反向解码,需API密钥。
- smartyStreet:专注美国的地址解码,需auth_id和auth_token。
- teleport:城市和地区地理解码服务。
- tomtom:TomTom地址解码,需API密钥。
- virtualearth:Bing Maps地理解码,需apiKey。
- yandex:Yandex地理解码服务,支持多语言。
每个提供者的详细配置和使用方法,请参考各自的服务文档和该库提供的接口说明。
获取选项
使用 options.fetch,您可以提供自己的数据获取方法。此方法应与Fetch API兼容。
这使您能够指定代理服务器以供使用、自定义超时时间、特定的头部信息等等。
格式化器
gpx:采用GPX格式来整理结果。string:将结果整理为字符串数组(需设定options.formatterPattern键)%P国家名称%p国家代码%n街道号码%S街道名称%z邮政编码%T州名%t州代码%c城市名称
更多资源
实验室
您可以在以下网址试用 node-geocoder:http://node-geocoder.herokuapp.com/
命令行工具
node-geocoder-cli 在shell中可通过该工具进行地理编码操作,详情参阅:https://github.com/nchaulet/node-geocoder-cli
扩展 node geocoder
扩展项目添加新的地理编码器,只需实现两个方法 geocode 和 reverse:
const geocoder = {
geocode: function(value, callback) { ... },
reverse: function(query, callback) { var lat = query.lat; var lon = query.lon; ... }
};
另外,也可以通过实现如下接口来自定义格式化器:
const formatter = {
format: function(data) {
return formattedData;
};
};
贡献指南
您可以为本项目增添更多地理编码器,以丰富功能。
运行测试很简单,只需执行 npm test 即可。
检查代码风格,仅需运行 npm run lint。