java实现北斗转GPS坐标
时间: 2023-09-18 20:08:08 浏览: 400
C++ GPS导航定位坐标转换程序.zip_GPS_GPS 转换_GPS导航坐标转换_导航坐标转换_导航定位
要将北斗坐标转换为GPS坐标,需要进行如下步骤:
1. 将北斗坐标系转换为地球坐标系;
2. 将地球坐标系转换为经纬度坐标系。
下面是Java代码实现:
```java
public class BeidouToGPS {
private static final double X_PI = 3.14159265358979324 * 3000.0 / 180.0;
private static final double A = 6378245.0;
private static final double EE = 0.00669342162296594323;
public static double[] bd09ToWGS84(double bd_lon, double bd_lat) {
double[] gcj = bd09ToGCJ02(bd_lon, bd_lat);
return gcjToWGS84(gcj[0], gcj[1]);
}
public static double[] bd09ToGCJ02(double bd_lon, double bd_lat) {
double x = bd_lon - 0.0065;
double y = bd_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_lon, gg_lat};
}
public static double[] gcjToWGS84(double lon, double lat) {
if (outOfChina(lon, lat)) {
return new double[]{lon, lat};
}
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * Math.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) * Math.PI);
dLon = (dLon * 180.0) / (A / sqrtMagic * Math.cos(radLat) * Math.PI);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new double[]{lon * 2 - mgLon, lat * 2 - mgLat};
}
private static boolean outOfChina(double lon, double lat) {
return lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271;
}
private static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
return ret;
}
}
```
其中,bd09ToWGS84方法用于将北斗坐标系转换为经纬度坐标系,bd09ToGCJ02方法用于将北斗坐标系转换为地球坐标系,gcjToWGS84方法用于将地球坐标系转换为经纬度坐标系。outOfChina方法用于判断经纬度坐标是否在中国境内,transformLat和transformLon方法用于进行坐标转换。调用bd09ToWGS84方法即可完成北斗坐标系到GPS坐标系的转换。
阅读全文