先上代码,用js实现的纠偏算法:
var pi = 3.14159265358979324;//// Krasovsky 1940//// a = 6378245.0, 1/f = 298.3// b = a * (1 - f)// ee = (a^2 - b^2) / a^2;var a = 6378245.0;var ee = 0.00669342162296594323;function outOfChina(lat, lon){if (lon < 72.004 || lon > 137.8347)return 1;if (lat < 0.8293 || lat > 55.8271)return 1;return 0;}function transformLat(x,y){var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(x > 0 ? x:-x);ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 *Math.sin(2.0 * x * pi)) * 2.0 / 3.0;ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;return ret;}function transformLon(x,y){var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(x > 0 ? x:-x);ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;return ret;}function transformFromWGSToGCJ(wgLoc){var mgLoc ={};mgLoc.lat = 0;mgLoc.lng = 0;if (outOfChina(wgLoc.lat, wgLoc.lng)){mgLoc = wgLoc;return mgLoc;}var dLat = transformLat(wgLoc.lng - 105.0, wgLoc.lat - 35.0);var dLon = transformLon(wgLoc.lng - 105.0, wgLoc.lat - 35.0);var radLat = wgLoc.lat / 180.0 * pi;var magic = Math.sin(radLat);magic = 1 - ee * magic * magic;var sqrtMagic = Math.sqrt(magic);dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);mgLoc.lat = wgLoc.lat + dLat;mgLoc.lng = wgLoc.lng + dLon;return mgLoc;}
参考文章1:/coolypf/article/details/8686588
WGS-84到GCJ-02的转换(即GPS加偏)算法是一个普通青年轻易无法接触到的“公开”的秘密。这个算法的代码在互联网上是公开的,详情请使用 Google 搜索"wgtochina_lb"。
整理后的算法代码请参考/SourceControl/changeset/view/21483#353936。知道了这个算法之后,就可以离线进行 Google 地图偏移校正,不必像之前那么麻烦。
至于 GCJ-02 到 WGS-84 的转换(即 GPS 纠偏),可以使用二分法。
参考文章2:/code/file/1665077235663872.htm?dir=/correct_lat_lon/src/com/hgq/correct/MapFix.java