cnblogs/dcrenl/JS经纬度坐标转换.html
2024-09-24 12:43:01 +08:00

152 lines
16 KiB
HTML

<div class="cnblogs_code">
<pre><span style="color: #0000ff;">var</span> GPS =<span style="color: #000000;"> {
PI : </span>3.14159265358979324<span style="color: #000000;">,
x_pi : </span>3.14159265358979324 * 3000.0 / 180.0<span style="color: #000000;">,
delta : </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (lat, lon) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Krasovsky 1940</span>
<span style="color: #008000;">//
</span> <span style="color: #008000;">//</span><span style="color: #008000;"> a = 6378245.0, 1/f = 298.3</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> b = a * (1 - f)</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> ee = (a^2 - b^2) / a^2;</span>
<span style="color: #0000ff;">var</span> a = 6378245.0; <span style="color: #008000;">//</span><span style="color: #008000;"> a: 卫星椭球坐标投影到平面地图坐标系的投影因子。</span>
<span style="color: #0000ff;">var</span> ee = 0.00669342162296594323; <span style="color: #008000;">//</span><span style="color: #008000;"> ee: 椭球的偏心率。</span>
<span style="color: #0000ff;">var</span> dLat = <span style="color: #0000ff;">this</span>.transformLat(lon - 105.0, lat - 35.0<span style="color: #000000;">);
</span><span style="color: #0000ff;">var</span> dLon = <span style="color: #0000ff;">this</span>.transformLon(lon - 105.0, lat - 35.0<span style="color: #000000;">);
</span><span style="color: #0000ff;">var</span> radLat = lat / 180.0 * <span style="color: #0000ff;">this</span><span style="color: #000000;">.PI;
</span><span style="color: #0000ff;">var</span> magic =<span style="color: #000000;"> Math.sin(radLat);
magic </span>= 1 - ee * magic *<span style="color: #000000;"> magic;
</span><span style="color: #0000ff;">var</span> sqrtMagic =<span style="color: #000000;"> Math.sqrt(magic);
dLat </span>= (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * <span style="color: #0000ff;">this</span><span style="color: #000000;">.PI);
dLon </span>= (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * <span style="color: #0000ff;">this</span><span style="color: #000000;">.PI);
</span><span style="color: #0000ff;">return</span> {'lat': dLat, 'lon'<span style="color: #000000;">: dLon};
},
</span><span style="color: #008000;">//</span><span style="color: #008000;">WGS-84 to GCJ-02</span>
gcj_encrypt : <span style="color: #0000ff;">function</span><span style="color: #000000;"> (wgsLat, wgsLon) {
</span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span><span style="color: #000000;">.outOfChina(wgsLat, wgsLon))
</span><span style="color: #0000ff;">return</span> {'lat': wgsLat, 'lon'<span style="color: #000000;">: wgsLon};
</span><span style="color: #0000ff;">var</span> d = <span style="color: #0000ff;">this</span><span style="color: #000000;">.delta(wgsLat, wgsLon);
</span><span style="color: #0000ff;">return</span> {'lat' : wgsLat + d.lat,'lon' : wgsLon +<span style="color: #000000;"> d.lon};
},
</span><span style="color: #008000;">//</span><span style="color: #008000;">GCJ-02 to WGS-84</span>
gcj_decrypt : <span style="color: #0000ff;">function</span><span style="color: #000000;"> (gcjLat, gcjLon) {
</span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span><span style="color: #000000;">.outOfChina(gcjLat, gcjLon))
</span><span style="color: #0000ff;">return</span> {'lat': gcjLat, 'lon'<span style="color: #000000;">: gcjLon};
</span><span style="color: #0000ff;">var</span> d = <span style="color: #0000ff;">this</span><span style="color: #000000;">.delta(gcjLat, gcjLon);
</span><span style="color: #0000ff;">return</span> {'lat': gcjLat - d.lat, 'lon': gcjLon -<span style="color: #000000;"> d.lon};
},
</span><span style="color: #008000;">//</span><span style="color: #008000;">GCJ-02 to WGS-84 exactly</span>
gcj_decrypt_exact : <span style="color: #0000ff;">function</span><span style="color: #000000;"> (gcjLat, gcjLon) {
</span><span style="color: #0000ff;">var</span> initDelta = 0.01<span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> threshold = 0.000000001<span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> dLat = initDelta, dLon =<span style="color: #000000;"> initDelta;
</span><span style="color: #0000ff;">var</span> mLat = gcjLat - dLat, mLon = gcjLon -<span style="color: #000000;"> dLon;
</span><span style="color: #0000ff;">var</span> pLat = gcjLat + dLat, pLon = gcjLon +<span style="color: #000000;"> dLon;
</span><span style="color: #0000ff;">var</span> wgsLat, wgsLon, i = 0<span style="color: #000000;">;
</span><span style="color: #0000ff;">while</span> (1<span style="color: #000000;">) {
wgsLat </span>= (mLat + pLat) / 2<span style="color: #000000;">;
wgsLon </span>= (mLon + pLon) / 2<span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> tmp = <span style="color: #0000ff;">this</span><span style="color: #000000;">.gcj_encrypt(wgsLat, wgsLon)
dLat </span>= tmp.lat -<span style="color: #000000;"> gcjLat;
dLon </span>= tmp.lon -<span style="color: #000000;"> gcjLon;
</span><span style="color: #0000ff;">if</span> ((Math.abs(dLat) &lt; threshold) &amp;&amp; (Math.abs(dLon) &lt;<span style="color: #000000;"> threshold))
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (dLat &gt; 0) pLat = wgsLat; <span style="color: #0000ff;">else</span> mLat =<span style="color: #000000;"> wgsLat;
</span><span style="color: #0000ff;">if</span> (dLon &gt; 0) pLon = wgsLon; <span style="color: #0000ff;">else</span> mLon =<span style="color: #000000;"> wgsLon;
</span><span style="color: #0000ff;">if</span> (++i &gt; 10000) <span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">console.log(i);</span>
<span style="color: #0000ff;">return</span> {'lat': wgsLat, 'lon'<span style="color: #000000;">: wgsLon};
},
</span><span style="color: #008000;">//</span><span style="color: #008000;">GCJ-02 to BD-09</span>
bd_encrypt : <span style="color: #0000ff;">function</span><span style="color: #000000;"> (gcjLat, gcjLon) {
</span><span style="color: #0000ff;">var</span> x = gcjLon, y =<span style="color: #000000;"> gcjLat;
</span><span style="color: #0000ff;">var</span> z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * <span style="color: #0000ff;">this</span><span style="color: #000000;">.x_pi);
</span><span style="color: #0000ff;">var</span> theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * <span style="color: #0000ff;">this</span><span style="color: #000000;">.x_pi);
bdLon </span>= z * Math.cos(theta) + 0.0065<span style="color: #000000;">;
bdLat </span>= z * Math.sin(theta) + 0.006<span style="color: #000000;">;
</span><span style="color: #0000ff;">return</span> {'lat' : bdLat,'lon'<span style="color: #000000;"> : bdLon};
},
</span><span style="color: #008000;">//</span><span style="color: #008000;">BD-09 to GCJ-02</span>
bd_decrypt : <span style="color: #0000ff;">function</span><span style="color: #000000;"> (bdLat, bdLon) {
</span><span style="color: #0000ff;">var</span> x = bdLon - 0.0065, y = bdLat - 0.006<span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * <span style="color: #0000ff;">this</span><span style="color: #000000;">.x_pi);
</span><span style="color: #0000ff;">var</span> theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * <span style="color: #0000ff;">this</span><span style="color: #000000;">.x_pi);
</span><span style="color: #0000ff;">var</span> gcjLon = z *<span style="color: #000000;"> Math.cos(theta);
</span><span style="color: #0000ff;">var</span> gcjLat = z *<span style="color: #000000;"> Math.sin(theta);
</span><span style="color: #0000ff;">return</span> {'lat' : gcjLat, 'lon'<span style="color: #000000;"> : gcjLon};
},
</span><span style="color: #008000;">//</span><span style="color: #008000;">WGS-84 to Web mercator</span>
<span style="color: #008000;">//</span><span style="color: #008000;">mercatorLat -&gt; y mercatorLon -&gt; x</span>
mercator_encrypt : <span style="color: #0000ff;">function</span><span style="color: #000000;">(wgsLat, wgsLon) {
</span><span style="color: #0000ff;">var</span> x = wgsLon * 20037508.34 / 180<span style="color: #000000;">.;
</span><span style="color: #0000ff;">var</span> y = Math.log(Math.tan((90. + wgsLat) * <span style="color: #0000ff;">this</span>.PI / 360.)) / (<span style="color: #0000ff;">this</span>.PI / 180<span style="color: #000000;">.);
y </span>= y * 20037508.34 / 180<span style="color: #000000;">.;
</span><span style="color: #0000ff;">return</span> {'lat' : y, 'lon'<span style="color: #000000;"> : x};
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
if ((Math.abs(wgsLon) &gt; 180 || Math.abs(wgsLat) &gt; 90))
return null;
var x = 6378137.0 * wgsLon * 0.017453292519943295;
var a = wgsLat * 0.017453292519943295;
var y = 3189068.5 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
return {'lat' : y, 'lon' : x};
//</span><span style="color: #008000;">*/</span><span style="color: #000000;">
},
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Web mercator to WGS-84</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> mercatorLat -&gt; y mercatorLon -&gt; x</span>
mercator_decrypt : <span style="color: #0000ff;">function</span><span style="color: #000000;">(mercatorLat, mercatorLon) {
</span><span style="color: #0000ff;">var</span> x = mercatorLon / 20037508.34 * 180<span style="color: #000000;">.;
</span><span style="color: #0000ff;">var</span> y = mercatorLat / 20037508.34 * 180<span style="color: #000000;">.;
y </span>= 180 / <span style="color: #0000ff;">this</span>.PI * (2 * Math.atan(Math.exp(y * <span style="color: #0000ff;">this</span>.PI / 180.)) - <span style="color: #0000ff;">this</span>.PI / 2<span style="color: #000000;">);
</span><span style="color: #0000ff;">return</span> {'lat' : y, 'lon'<span style="color: #000000;"> : x};
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
if (Math.abs(mercatorLon) &lt; 180 &amp;&amp; Math.abs(mercatorLat) &lt; 90)
return null;
if ((Math.abs(mercatorLon) &gt; 20037508.3427892) || (Math.abs(mercatorLat) &gt; 20037508.3427892))
return null;
var a = mercatorLon / 6378137.0 * 57.295779513082323;
var x = a - (Math.floor(((a + 180.0) / 360.0)) * 360.0);
var y = (1.5707963267948966 - (2.0 * Math.atan(Math.exp((-1.0 * mercatorLat) / 6378137.0)))) * 57.295779513082323;
return {'lat' : y, 'lon' : x};
//</span><span style="color: #008000;">*/</span><span style="color: #000000;">
},
</span><span style="color: #008000;">//</span><span style="color: #008000;"> two point's distance</span>
distance : <span style="color: #0000ff;">function</span><span style="color: #000000;"> (latA, lonA, latB, lonB) {
</span><span style="color: #0000ff;">var</span> earthR = 6371000<span style="color: #000000;">.;
</span><span style="color: #0000ff;">var</span> x = Math.cos(latA * <span style="color: #0000ff;">this</span>.PI / 180.) * Math.cos(latB * <span style="color: #0000ff;">this</span>.PI / 180.) * Math.cos((lonA - lonB) * <span style="color: #0000ff;">this</span>.PI / 180<span style="color: #000000;">);
</span><span style="color: #0000ff;">var</span> y = Math.sin(latA * <span style="color: #0000ff;">this</span>.PI / 180.) * Math.sin(latB * <span style="color: #0000ff;">this</span>.PI / 180<span style="color: #000000;">.);
</span><span style="color: #0000ff;">var</span> s = x +<span style="color: #000000;"> y;
</span><span style="color: #0000ff;">if</span> (s &gt; 1) s = 1<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (s &lt; -1) s = -1<span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> alpha =<span style="color: #000000;"> Math.acos(s);
</span><span style="color: #0000ff;">var</span> distance = alpha *<span style="color: #000000;"> earthR;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> distance;
},
outOfChina : </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (lat, lon) {
</span><span style="color: #0000ff;">if</span> (lon &lt; 72.004 || lon &gt; 137.8347<span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (lat &lt; 0.8293 || lat &gt; 55.8271<span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
},
transformLat : </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (x, y) {
</span><span style="color: #0000ff;">var</span> ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 *<span style="color: #000000;"> Math.sqrt(Math.abs(x));
ret </span>+= (20.0 * Math.sin(6.0 * x * <span style="color: #0000ff;">this</span>.PI) + 20.0 * Math.sin(2.0 * x * <span style="color: #0000ff;">this</span>.PI)) * 2.0 / 3.0<span style="color: #000000;">;
ret </span>+= (20.0 * Math.sin(y * <span style="color: #0000ff;">this</span>.PI) + 40.0 * Math.sin(y / 3.0 * <span style="color: #0000ff;">this</span>.PI)) * 2.0 / 3.0<span style="color: #000000;">;
ret </span>+= (160.0 * Math.sin(y / 12.0 * <span style="color: #0000ff;">this</span>.PI) + 320 * Math.sin(y * <span style="color: #0000ff;">this</span>.PI / 30.0)) * 2.0 / 3.0<span style="color: #000000;">;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ret;
},
transformLon : </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (x, y) {
</span><span style="color: #0000ff;">var</span> ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 *<span style="color: #000000;"> Math.sqrt(Math.abs(x));
ret </span>+= (20.0 * Math.sin(6.0 * x * <span style="color: #0000ff;">this</span>.PI) + 20.0 * Math.sin(2.0 * x * <span style="color: #0000ff;">this</span>.PI)) * 2.0 / 3.0<span style="color: #000000;">;
ret </span>+= (20.0 * Math.sin(x * <span style="color: #0000ff;">this</span>.PI) + 40.0 * Math.sin(x / 3.0 * <span style="color: #0000ff;">this</span>.PI)) * 2.0 / 3.0<span style="color: #000000;">;
ret </span>+= (150.0 * Math.sin(x / 12.0 * <span style="color: #0000ff;">this</span>.PI) + 300.0 * Math.sin(x / 30.0 * <span style="color: #0000ff;">this</span>.PI)) * 2.0 / 3.0<span style="color: #000000;">;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ret;
}
};</span></pre>
</div>
<p>&nbsp;</p>