カスタム関数でgpsを高徳座標に変換する
22567 ワード
php
echo<<<_END
<span style="color:#0000ff;">var</span> GPS =<span style="color:#000000;"> {
</span><span style="color:#008080;">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> (lat,<span style="color:#000000;"> lon) {
</span><span style="color:#008000;">//</span><span style="color:#008000;"> Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// 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 = this.transformLat(lon - 105.0, lat - 35.0<span style="color:#000000;">);
</span><span style="color:#0000ff;">var</span> dLon = this.transformLon(lon - 105.0, lat - 35.0<span style="color:#000000;">);
</span><span style="color:#0000ff;">var</span> radLat = lat / 180.0 * this.<span style="color:#008080;">PI</span><span style="color:#000000;">;
</span><span style="color:#0000ff;">var</span> magic = Math.<span style="color:#008080;">sin</span><span style="color:#000000;">(radLat);
magic </span>= 1 - ee * magic *<span style="color:#000000;"> magic;
</span><span style="color:#0000ff;">var</span> sqrtMagic = Math.<span style="color:#008080;">sqrt</span><span style="color:#000000;">(magic);
dLat </span>= (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.<span style="color:#008080;">PI</span><span style="color:#000000;">);
dLon </span>= (dLon * 180.0) / (a / sqrtMagic * Math.<span style="color:#008080;">cos</span>(radLat) * this.<span style="color:#008080;">PI</span><span style="color:#000000;">);
</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> (wgsLat,<span style="color:#000000;"> wgsLon) {
</span><span style="color:#0000ff;">if</span> (this.outOfChina(wgsLat,<span style="color:#000000;"> wgsLon))
</span><span style="color:#0000ff;">return</span> {'lat': wgsLat, 'lon':<span style="color:#000000;"> wgsLon};
</span><span style="color:#0000ff;">var</span> d = this.delta(wgsLat,<span style="color:#000000;"> wgsLon);
</span><span style="color:#0000ff;">return</span> {'lat' : wgsLat + d.lat,'lon' : wgsLon + d.<span style="color:#000000;">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> (gcjLat,<span style="color:#000000;"> gcjLon) {
</span><span style="color:#0000ff;">if</span> (this.outOfChina(gcjLat,<span style="color:#000000;"> gcjLon))
</span><span style="color:#0000ff;">return</span> {'lat': gcjLat, 'lon':<span style="color:#000000;"> gcjLon};
</span><span style="color:#0000ff;">var</span> d = this.delta(gcjLat,<span style="color:#000000;"> gcjLon);
</span><span style="color:#0000ff;">return</span> {'lat': gcjLat - d.lat, 'lon': gcjLon - d.<span style="color:#000000;">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> (gcjLat,<span style="color:#000000;"> 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 = this.gcj_encrypt(wgsLat,<span style="color:#000000;"> 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.<span style="color:#008080;">abs</span>(dLat) < threshold) && (Math.<span style="color:#008080;">abs</span>(dLon) <<span style="color:#000000;"> threshold))
</span><span style="color:#0000ff;">break</span><span style="color:#000000;">;
</span><span style="color:#0000ff;">if</span> (dLat > 0) pLat = wgsLat; <span style="color:#0000ff;">else</span> mLat =<span style="color:#000000;"> wgsLat;
</span><span style="color:#0000ff;">if</span> (dLon > 0) pLon = wgsLon; <span style="color:#0000ff;">else</span> mLon =<span style="color:#000000;"> wgsLon;
</span><span style="color:#0000ff;">if</span> (++i > 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> (gcjLat,<span style="color:#000000;"> 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.<span style="color:#008080;">sqrt</span>(x * x + y * y) + 0.00002 * Math.<span style="color:#008080;">sin</span>(y * this.<span style="color:#000000;">x_pi);
</span><span style="color:#0000ff;">var</span> theta = Math.<span style="color:#008080;">atan2</span>(y, x) + 0.000003 * Math.<span style="color:#008080;">cos</span>(x * this.<span style="color:#000000;">x_pi);
bdLon </span>= z * Math.<span style="color:#008080;">cos</span>(theta) + 0.0065<span style="color:#000000;">;
bdLat </span>= z * Math.<span style="color:#008080;">sin</span>(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> (bdLat,<span style="color:#000000;"> 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.<span style="color:#008080;">sqrt</span>(x * x + y * y) - 0.00002 * Math.<span style="color:#008080;">sin</span>(y * this.<span style="color:#000000;">x_pi);
</span><span style="color:#0000ff;">var</span> theta = Math.<span style="color:#008080;">atan2</span>(y, x) - 0.000003 * Math.<span style="color:#008080;">cos</span>(x * this.<span style="color:#000000;">x_pi);
</span><span style="color:#0000ff;">var</span> gcjLon = z * Math.<span style="color:#008080;">cos</span><span style="color:#000000;">(theta);
</span><span style="color:#0000ff;">var</span> gcjLat = z * Math.<span style="color:#008080;">sin</span><span style="color:#000000;">(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
//mercatorLat -> y mercatorLon -> x</span>
mercator_encrypt : <span style="color:#0000ff;">function</span>(wgsLat,<span style="color:#000000;"> 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.<span style="color:#008080;">log</span>(Math.<span style="color:#008080;">tan</span>((90. + wgsLat) * this.<span style="color:#008080;">PI</span> / 360.)) / (this.<span style="color:#008080;">PI</span> / 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) > 180 || Math.abs(wgsLat) > 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
// mercatorLat -> y mercatorLon -> x</span>
mercator_decrypt : <span style="color:#0000ff;">function</span>(mercatorLat,<span style="color:#000000;"> 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 / this.<span style="color:#008080;">PI</span> * (2 * Math.<span style="color:#008080;">atan</span>(Math.<span style="color:#008080;">exp</span>(y * this.<span style="color:#008080;">PI</span> / 180.)) - this.<span style="color:#008080;">PI</span> / 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) < 180 && Math.abs(mercatorLat) < 90)
return null;
if ((Math.abs(mercatorLon) > 20037508.3427892) || (Math.abs(mercatorLat) > 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> (latA, lonA, latB,<span style="color:#000000;"> lonB) {
</span><span style="color:#0000ff;">var</span> earthR = 6371000.<span style="color:#000000;">;
</span><span style="color:#0000ff;">var</span> x = Math.<span style="color:#008080;">cos</span>(latA * this.<span style="color:#008080;">PI</span> / 180.) * Math.<span style="color:#008080;">cos</span>(latB * this.<span style="color:#008080;">PI</span> / 180.) * Math.<span style="color:#008080;">cos</span>((lonA - lonB) * this.<span style="color:#008080;">PI</span> / 180<span style="color:#000000;">);
</span><span style="color:#0000ff;">var</span> y = Math.<span style="color:#008080;">sin</span>(latA * this.<span style="color:#008080;">PI</span> / 180.) * Math.<span style="color:#008080;">sin</span>(latB * this.<span style="color:#008080;">PI</span> / 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 > 1) s = 1<span style="color:#000000;">;
</span><span style="color:#0000ff;">if</span> (s < -1) s = -1<span style="color:#000000;">;
</span><span style="color:#0000ff;">var</span> alpha = Math.<span style="color:#008080;">acos</span><span style="color:#000000;">(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;
}</span>,<span style="color:#000000;">
outOfChina </span>: <span style="color:#0000ff;">function</span> (lat,<span style="color:#000000;"> lon) {
</span><span style="color:#0000ff;">if</span> (lon < 72.004 || lon > 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 < 0.8293 || lat > 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;">;
}</span>,<span style="color:#000000;">
transformLat </span>: <span style="color:#0000ff;">function</span> (x,<span style="color:#000000;"> 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 * Math.<span style="color:#008080;">sqrt</span>(Math.<span style="color:#008080;">abs</span><span style="color:#000000;">(x));
ret </span>+= (20.0 * Math.<span style="color:#008080;">sin</span>(6.0 * x * this.<span style="color:#008080;">PI</span>) + 20.0 * Math.<span style="color:#008080;">sin</span>(2.0 * x * this.<span style="color:#008080;">PI</span>)) * 2.0 / 3.0<span style="color:#000000;">;
ret </span>+= (20.0 * Math.<span style="color:#008080;">sin</span>(y * this.<span style="color:#008080;">PI</span>) + 40.0 * Math.<span style="color:#008080;">sin</span>(y / 3.0 * this.<span style="color:#008080;">PI</span>)) * 2.0 / 3.0<span style="color:#000000;">;
ret </span>+= (160.0 * Math.<span style="color:#008080;">sin</span>(y / 12.0 * this.<span style="color:#008080;">PI</span>) + 320 * Math.<span style="color:#008080;">sin</span>(y * this.<span style="color:#008080;">PI</span> / 30.0)) * 2.0 / 3.0<span style="color:#000000;">;
</span><span style="color:#0000ff;">return</span><span style="color:#000000;"> ret;
}</span>,<span style="color:#000000;">
transformLon </span>: <span style="color:#0000ff;">function</span> (x,<span style="color:#000000;"> 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 * Math.<span style="color:#008080;">sqrt</span>(Math.<span style="color:#008080;">abs</span><span style="color:#000000;">(x));
ret </span>+= (20.0 * Math.<span style="color:#008080;">sin</span>(6.0 * x * this.<span style="color:#008080;">PI</span>) + 20.0 * Math.<span style="color:#008080;">sin</span>(2.0 * x * this.<span style="color:#008080;">PI</span>)) * 2.0 / 3.0<span style="color:#000000;">;
ret </span>+= (20.0 * Math.<span style="color:#008080;">sin</span>(x * this.<span style="color:#008080;">PI</span>) + 40.0 * Math.<span style="color:#008080;">sin</span>(x / 3.0 * this.<span style="color:#008080;">PI</span>)) * 2.0 / 3.0<span style="color:#000000;">;
ret </span>+= (150.0 * Math.<span style="color:#008080;">sin</span>(x / 12.0 * this.<span style="color:#008080;">PI</span>) + 300.0 * Math.<span style="color:#008080;">sin</span>(x / 30.0 * this.<span style="color:#008080;">PI</span>)) * 2.0 / 3.0<span style="color:#000000;">;
</span><span style="color:#0000ff;">return</span><span style="color:#000000;"> ret;
}
};
document</span>.write("GPS: 45.702363888889, 126.60580555556<br />"<span style="color:#000000;">);
</span><span style="color:#0000ff;">var</span> arr2 = GPS.gcj_encrypt(45.702363888889, 126.60580555556<span style="color:#000000;">);
document</span>.write(" :" + arr2['lat']+","+arr2['lon']+'<br />'<span style="color:#000000;">);
</span><span style="color:#0000ff;">var</span> myArray=[126.60580555556,45.702363888889<span style="color:#000000;">];
AMap</span>.convertFrom(myArray, 'GPS', <span style="color:#0000ff;">function</span>(status,<span style="color:#000000;"> result) {
alert(result</span>.locations[0<span style="color:#000000;">]);
</span><span style="color:#008000;">//</span><span style="color:#008000;">document.write(" :" + result.locations[0].lat+","+result.locations[0].lng+'<br />');</span>
<span style="color:#000000;">})
</span>
_END;
?>
転載先:https://www.cnblogs.com/vactor/p/6683723.html