前端开发中,经常需要使用地图信息,而地图信息与地理位置之间的转换便需要使用 geocoder 包。geocoder-arcgis 是一个 npm 包,提供了基于 arcgis 地理信息系统(GIS)进行 geocoder 的方法。本文将介绍如何使用 geocoder-arcgis 包。
安装
首先,使用 npm 安装 geocoder-arcgis:
npm install geocoder-arcgis
安装完成后,即可在项目中使用该包。
使用
创建 geocoder 对象
使用以下代码创建 geocoder 对象:
const geocoder = require('geocoder-arcgis')({ endpoint: "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer", outFields: 'Match_addr,Addr_type', token: "YOUR_TOKEN" });
其中,endpoint
属性指定了使用的 arcgis 地址信息服务地址,outFields
属性指定了需要返回的字段,token
属性指定了访问该服务的访问令牌。
geocode
使用 geocode
方法进行地址解析,在函数中传入地理位置信息,如:
geocoder.geocode("1600 Pennsylvania Ave NW, Washington, DC 20500", (error, response) => { console.log(response); });
geocode
方法返回的是一个回调函数,第一个参数为错误信息,第二个参数为处理后的地理位置信息。
reverse
使用 reverse
方法进行地址反向解析,即将地理位置的经纬度转换为对应的地址信息:
geocoder.reverse({lat: 38.8976099, lon: -77.036734}, (error, response) => { console.log(response); });
同样,reverse
方法返回的也是一个回调函数,第一个参数为错误信息,第二个参数为处理后的地址信息。
示例代码
-- -------------------- ---- ------- ----- -------- - ---------------------------- --------- ---------------------------------------------------------------------- ---------- ----------------------- ------ ------------ --- ---------------------- ------------ --- --- ----------- -- ------- ------- --------- -- - ---------------------- --- ---------------------- ----------- ---- ------------ ------- --------- -- - ---------------------- ---展开代码
结论
npm 包 geocoder-arcgis 是一个便捷的地址解析和反向解析工具,与 arcgis 地理信息系统进行深度集成,并提供了标准的 api 接口。通过本文的介绍,读者可以快速掌握该 npm 包的使用方法。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/5eedae65b5cbfe1ea0610e2b