介绍
geo-convert-position-format是一个JavaScript库,用于将不同格式的地理位置坐标互相转换。它支持许多常用的格式,如WGS84、GCJ02、BD09等,还支持不同的精度。
注意:geo-convert-position-format依赖于ES6支持。如果您的目标平台不支持ES6,请使用
Babel
转换代码。
安装
使用npm安装geo-convert-position-format。
- --- ------- ---------------------------
使用
首先,导入geo-convert-position-format。
------ - -------- - ---- ------------------------------
然后,您可以使用以下方法将坐标从一种格式转换为另一种格式:
Position.fromWGS84(lat, lng)
将WGS84格式的坐标转换为内部格式。
----- --- - ------------------------- --------
Position.fromGCJ02(lat, lng)
将GCJ02格式的坐标转换为内部格式。
----- --- - ------------------------- --------
Position.fromBD09(lat, lng)
将BD09格式的坐标转换为内部格式。
----- --- - ------------------------ --------
Position.fromArray(arr)
从数组中构造一个位置。
----- --- - --------------------------- --------
Position.fromString(str)
从字符串中构造一个位置。
----- --- - ------------------------------------
pos.toWGS84()
将内部格式的坐标转换为WGS84格式。
----- ----- - --------------
pos.toGCJ02()
将内部格式的坐标转换为GCJ02格式。
----- ----- - --------------
pos.toBD09()
将内部格式的坐标转换为BD09格式。
----- ---- - -------------
pos.toArray()
将内部格式的坐标转换为数组。
----- --- - -------------- -- -------- ------
pos.toString()
将内部格式的坐标转换为字符串。
----- --- - --------------- -- --------------
示例代码
------ - -------- - ---- ------------------------------ ----- --- - ------------------------- -------- ----- ----- - -------------- ------------------- -- --------------- ------------- ----- ---- - ------------- ------------------ -- --------------- ------------- ----- --- - -------------- ----------------- -- -------- ------ ----- --- - --------------- ----------------- -- --------------
结论
geo-convert-position-format是一个功能强大的JavaScript库,可以方便地将地理位置坐标转换为不同的格式。其清晰的API和易于使用的方法使其成为前端开发人员的首选。
来源:JavaScript中文网 ,转载请联系管理员! 本文地址:https://www.javascriptcn.com/post/5ef29fb68c4ce90ee4ca3b55