WGS坐标系是国际通用的一种地心坐标系,WGS本身也有多个版本(不赘述了),GCJ-02是国内官方采用的一种坐标系,国内许多坐标系也是基于GCJ-02变种而来的,比如百度坐标系BD-09。就这么简单介绍一下吧,坐标系本身的算法是比较复杂的,需要了解空间地理知识,百度文库里面也有一些坐标纠偏转换算法的相关论文,有兴趣的可以自己去研究。
static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
public final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
/**
*
* @Title: wgs2bd
* @Description: 84to火星坐标系(GCJ-02)World Geodetic System==>Mars Geodetic System
* 火星坐标系(GCJ-02)与百度坐标系(BD-09)的转换算法将GCJ-02坐标转换成BD-0 坐标
* (1. WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
* 谷歌地图采用的是WGS84地理坐标系(中国范围除外);
* 2.BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系;)
* @param lat
* @param lon
* @return
*/
public static double[] wgs2bd(double lat, double lon) {
double[] wgs2gcj = wgs2gcj(lat, lon);
double[] gcj2bd = gcj2bd(wgs2gcj[0], wgs2gcj[1]);
return gcj2bd;
}
/**
*
* @Title: gcj2bd
* @Description:火星坐标系(GCJ-02)与百度坐标系(BD-09)的转换算法,将GCJ-02坐标转换成BD-09坐标
* @param lat
* @param lon
* @return
*/
public static double[] gcj2bd(double lat, double lon) {
double x = lon, y = lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
double bd_lon = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new double[] { bd_lat, bd_lon };
}
/**
*
* @Title: gcj2bd
* @Description: 百度坐标系(BD-09)与火星坐标系(GCJ-02)的转换算法,将BD-09坐标转换成GCJ-02坐标
* @param lat
* @param lon
* @return
*/
public static double[] bd2gcj(double lat, double lon) {
double x = lon - 0.0065, y = lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
double gg_lon = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new double[] { gg_lat, gg_lon };
}
/**
*
* @Title: wgs2gcj
* @Description: 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
* @param lat
* @param lon
* @return
*/
public static double[] wgs2gcj(double lat, double lon) {
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
double[] loc = { mgLat, mgLon };
return loc;
}
private static double transformLat(double lat, double lon) {
double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat));
ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double transformLon(double lat, double lon) {
double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat));
ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
pi: 圆周率。
a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
ee: 椭球的偏心率。
x_pi: 圆周率转换量。
transformLat(lat, lon): 转换方法,比较复杂,不必深究了。输入:横纵坐标,输出:转换后的横坐标。
transformLon(lat, lon): 转换方法,同样复杂,自行脑补吧。输入:横纵坐标,输出:转换后的纵坐标。
wgs2gcj(lat, lon): WGS坐标转换为GCJ坐标。
gcj2bd(lat, lon): GCJ坐标转换为百度坐标。