js 批量坐标转换经纬度_JS经纬度坐标转换 挺准确的

该博客详细介绍了如何使用JavaScript进行坐标转换,包括从GCJ-02(中国常用)到WGS-84(全球标准)的精确解密,以及从GCJ-02到BD-09(百度坐标系)的加密和解密方法。还提供了批量处理和误差修正的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 data() {2 return {3 PI: 3.14159265358979324,4 x_pi: (3.14159265358979324 * 3000.0) / 180.05 }6 },7 methods: {8 delta(lat, lon) {9 // Krasovsky 194010 //11 // a = 6378245.0, 1/f = 298.312 // b = a * (1 - f)13 // ee = (a^2 - b^2) / a^214 const a = 6378245.0 // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。15 const ee = 0.00669342162296594323 // ee: 椭球的偏心率。16 let dLat = this.transformLat(lon - 105.0, lat - 35.0)17 let dLon = this.transformLon(lon - 105.0, lat - 35.0)18 const radLat = (lat / 180.0) * this.PI19 let magic = Math.sin(radLat)20 magic = 1 - ee * magic * magic21 const sqrtMagic = Math.sqrt(magic)22 dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * this.PI)23 dLon = (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * this.PI)24 return { lat: dLat, lon: dLon }25 },26 // WGS-84 to GCJ-0227 gcj_encrypt(wgsLat, wgsLon) {28 if (this.outOfChina(wgsLat, wgsLon)) {29 return { lat: wgsLat, lon: wgsLon }30 }31 const d = this.delta(wgsLat, wgsLon)32 return { lat: wgsLat + d.lat, lon: wgsLon + d.lon }33 },34 // GCJ-02 to WGS-8435 gcj_decrypt(gcjLat, gcjLon) {36 if (this.outOfChina(gcjLat, gcjLon)) {37 return { lat: gcjLat, lon: gcjLon }38 }39 const d = this.delta(gcjLat, gcjLon)40 return { lat: gcjLat - d.lat, lon: gcjLon - d.lon }41 },42 // GCJ-02 to WGS-84 exactly43 gcj_decrypt_exact(gcjLat, gcjLon) {44 const initDelta = 0.0145 const threshold = 0.00000000146 let dLat = initDelta47 let dLon = initDelta48 let mLat = gcjLat - dLat49 let mLon = gcjLon - dLon50 let pLat = gcjLat + dLat51 let pLon = gcjLon + dLon52 let wgsLat53 let wgsLon54 let i = 055 while (1) {56 wgsLat = (mLat + pLat) / 257 wgsLon = (mLon + pLon) / 258 const tmp = this.gcj_encrypt(wgsLat, wgsLon)59 dLat = tmp.lat - gcjLat60 dLon = tmp.lon - gcjLon61 if (Math.abs(dLat)

65 if (dLat>0) {66 pLat = wgsLat67 } else {68 mLat = wgsLat69 }70 if (dLon > 0) {71 pLon = wgsLon72 } else {73 mLon = wgsLon74 }75 if (++i > 10000) {76 break77 }78 }79 // console.log(i)80 return { lat: wgsLat, lon: wgsLon }81 },82 // GCJ-02 to BD-0983 bd_encrypt(gcjLat, gcjLon) {84 const x = gcjLon85 const y = gcjLat86 const z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * this.x_pi)87 const theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * this.x_pi)88 const bdLon = z * Math.cos(theta) + 0.006589 const bdLat = z * Math.sin(theta) + 0.00690 return { lat: bdLat, lon: bdLon }91 },92 // BD-09 to GCJ-0293 bd_decrypt(bdLat, bdLon) {94 const x = bdLon - 0.006595 const y = bdLat - 0.00696 const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * this.x_pi)97 const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * this.x_pi)98 const gcjLon = z * Math.cos(theta)99 const gcjLat = z * Math.sin(theta)100 return { lat: gcjLat, lon: gcjLon }101 },102 // WGS-84 to Web mercator103 // mercatorLat -> y mercatorLon -> x104 mercator_encrypt: function(wgsLat, wgsLon) {105 const x = (wgsLon * 20037508.34) / 180.0106 let y =107 Math.log(Math.tan(((90.0 + wgsLat) * this.PI) / 360.0)) /108 (this.PI / 180.0)109 y = (y * 20037508.34) / 180.0110 return { lat: y, lon: x }111 /*112 if ((Math.abs(wgsLon) > 180 || Math.abs(wgsLat) > 90))113 return null114 var x = 6378137.0 * wgsLon * 0.017453292519943295115 var a = wgsLat * 0.017453292519943295116 var y = 3189068.5 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)))117 return {'lat' : y, 'lon' : x}118 //*/119 },120 // Web mercator to WGS-84121 // mercatorLat -> y mercatorLon -> x122 mercator_decrypt: function(mercatorLat, mercatorLon) {123 const x = (mercatorLon / 20037508.34) * 180.0124 let y = (mercatorLat / 20037508.34) * 180.0125 y =126 (180 / this.PI) *127 (2 * Math.atan(Math.exp((y * this.PI) / 180.0)) - this.PI / 2)128 return { lat: y, lon: x }129 /*130 if (Math.abs(mercatorLon) <180&& Math.abs(mercatorLat) < 90)131 return null132 if ((Math.abs(mercatorLon)>20037508.3427892) || (Math.abs(mercatorLat) > 20037508.3427892))133 return null134 var a = mercatorLon / 6378137.0 * 57.295779513082323135 var x = a - (Math.floor(((a + 180.0) / 360.0)) * 360.0)136 var y = (1.5707963267948966 - (2.0 * Math.atan(Math.exp((-1.0 * mercatorLat) / 6378137.0)))) * 57.295779513082323137 return {'lat' : y, 'lon' : x}138 //*/139 },140 // two point's distance141 distance(latA, lonA, latB, lonB) {142 const earthR = 6371000.0143 const x =144 Math.cos((latA * this.PI) / 180.0) *145 Math.cos((latB * this.PI) / 180.0) *146 Math.cos(((lonA - lonB) * this.PI) / 180)147 const y =148 Math.sin((latA * this.PI) / 180.0) * Math.sin((latB * this.PI) / 180.0)149 let s = x + y150 if (s > 1) {151 s = 1152 }153 if (s 137.8347) {162 return true163 }164 if (lat <0.8293 || lat>55.8271) {165 return true166 }167 return false168 },169 transformLat(x, y) {170 let ret =171 -100.0 +172 2.0 * x +173 3.0 * y +174 0.2 * y * y +175 0.1 * x * y +176 0.2 * Math.sqrt(Math.abs(x))177 ret +=178 ((20.0 * Math.sin(6.0 * x * this.PI) +179 20.0 * Math.sin(2.0 * x * this.PI)) *180 2.0) /181 3.0182 ret +=183 ((20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin((y / 3.0) * this.PI)) *184 2.0) /185 3.0186 ret +=187 ((160.0 * Math.sin((y / 12.0) * this.PI) +188 320 * Math.sin((y * this.PI) / 30.0)) *189 2.0) /190 3.0191 return ret192 },193 transformLon(x, y) {194 let ret =195 300.0 +196 x +197 2.0 * y +198 0.1 * x * x +199 0.1 * x * y +200 0.1 * Math.sqrt(Math.abs(x))201 ret +=202 ((20.0 * Math.sin(6.0 * x * this.PI) +203 20.0 * Math.sin(2.0 * x * this.PI)) *204 2.0) /205 3.0206 ret +=207 ((20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin((x / 3.0) * this.PI)) *208 2.0) /209 3.0210 ret +=211 ((150.0 * Math.sin((x / 12.0) * this.PI) +212 300.0 * Math.sin((x / 30.0) * this.PI)) *213 2.0) /214 3.0215 return ret216 }217 }218 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值