/**
* 坐标工具
*
* @author Cheng
* @date 2021/04/07
*/
public class CoordinatesUtil {
public static double pi = 3.1415926535897932384626;
public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
public static double a = 6378245.0;
public static double ee = 0.00669342162296594323;
/**
* 84 ==》 高德
*
* @param lon 经度
* @param lat 纬度
* @return
*/
public static String gps84_To_Gcj02(double lon, double lat) {
if (outOfChina(lat, lon)) {
return 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 * 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;
return mgLon + "," + mgLat;
}
/**
* 高德 ==》 84
*
* @param lon * @param lat * @return
* @param lat 纬度
* @param lon 经度
*/
public static String gcj02_To_Gps84(double lon, double lat) {
double[] gps = transform(lat, lon);
double lontitude = lon * 2 - gps[1];
double latitude = lat * 2 - gps[0];
return lontitude + "," + latitude;
}
/**
* 百度转高德
*
* @param bd_lon 经度
* @param bd_lat 纬度
* @return {@link String}
*/
private static String bdToGaoDe(double bd_lon, double bd_lat) {
double[] gd_lat_lon = new double[2];
double PI = 3.14159265358979324 * 3000.0 / 180.0;
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
gd_lat_lon[0] = z * Math.cos(theta);
gd_lat_lon[1] = z * Math.sin(theta);
return gd_lat_lon[0] + "," + gd_lat_lon[1];
}
/**
* 百度转84
*
* @param lon 经度
* @param lat 纬度
* @return {@link String}
*/
public static String bd09_To_gps84(double lon, double lat) {
String[] gcj02 = bdToGaoDe(lon, lat).split(",");
return gcj02_To_Gps84(Double.parseDouble(gcj02[0]), Double.parseDouble(gcj02[1]));
}
/**
* 高德 == 》 百度
*
* @param lon 经度
* @param lat 纬度
*/
public static String gcj02_To_Bd09(double lon, double lat) {
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 tempLon = z * Math.cos(theta) + 0.0065;
double tempLat = z * Math.sin(theta) + 0.006;
return tempLon + "," + tempLat;
}
/**
* 84 == 》 百度
*
* @param lat 纬度
* @param lon 经度
* @return
*/
public static String gps84_To_bd09(double lon, double lat) {
String[] Gcj02 = gps84_To_Gcj02(lon, lat).split(",");
return gcj02_To_Bd09(Double.parseDouble(Gcj02[0]), Double.parseDouble(Gcj02[1]));
}
/**
* wgs84to墨卡托
*
* @param lng 经度
* @param lat 纬度
* @return {@link String}
*/
public static String wgs84toMercator(double lng, double lat) {
Map<String, Object> toMercator = new HashMap(3);
toMercator.put("coords", lng + "," + lat);
toMercator.put("from", 1);
toMercator.put("to", 6);
toMercator.put("ak", "UpCmCBcWeKShAgw46m6xwxT5QmBssBKy");
try {
return HttpUtils.httpGetRequest("http://api.map.baidu.com/geoconv/v1/", toMercator);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
/**
* 判断是否在中国
*
* @param lat
* @param lon
* @return
*/
public static boolean outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347) {
return true;
}
return lat < 0.8293 || lat > 55.8271;
}
public 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 * 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;
}
public 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 * 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;
}
public static double[] transform(double lat, double lon) {
if (outOfChina(lat, lon)) {
return new double[]{lat, 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;
return new double[]{mgLat, mgLon};
}
}
发表时间:2021-08-25
本站文章除注明转载/出处外,皆为作者原创,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。