Java【代码分享 04】坐标系说明+WGS84 GCJ02 BD09坐标系转换工具+Java坐标系转换及验证源代码分享(粘贴可用(1)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以点击这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

* A WGS 长轴半径
*/
private static final double A = 6378245.0D;

/\*\*

* EE WGS 偏心率的平方
*/
private static final double EE = 0.00669342162296594323D;

/\*\*

* WGS84转换GCJ02适用于继承的情况
*
* @param t 继承TransformPoint类的子类对象
*/
public static void wgs84ToGcj02(T t) {
wgs84ToGcj02(t::getLng, t::getLat, t::setTransformLng, t::setTransformLat);
}

/\*\*

* WGS84转换GCJ02核心方法
*
* @param fromLon 转换前的经度
* @param fromLat 转换前的纬度
* @return 转换后的经纬度map对象
*/
public static Map<String, Double> wgs84ToGcj02(double fromLon, double fromLat) {
HashMap<String, Double> transformRes = new HashMap<>(2);
// 国外坐标不用进行加密
if (outOfChina(fromLon, fromLat)) {
transformRes.put(“lon”, fromLon);
transformRes.put(“lat”, fromLat);
return transformRes;
}
// 计算转换后的经纬度坐标
double dLat = transformLat(fromLon - 105.0, fromLat - 35.0);
double dLon = transformLon(fromLon - 105.0, fromLat - 35.0);
double radLat = fromLat / 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 = fromLat + dLat;
double mgLon = fromLon + dLon;
transformRes.put(“lon”, new BigDecimal(mgLon + “”, new MathContext(9, RoundingMode.HALF_UP)).doubleValue());
transformRes.put(“lat”, new BigDecimal(mgLat + “”, new MathContext(9, RoundingMode.HALF_UP)).doubleValue());

    return transformRes;
}

/\*\*

* GCJ02转换WGS84
*
* @param lon 转换前的经度
* @param lat 转换后的纬度
* @return 转换后的经纬度map对象
*/
public static Map<String, Double> gcj02ToWgs84(double lon, double lat) {
Map<String, Double> transformRes = new HashMap<>(2);
double longitude = lon * 2 - wgs84ToGcj02(lon, lat).get(“lon”);
double latitude = lat * 2 - wgs84ToGcj02(lon, lat).get(“lat”);
transformRes.put(“lon”, longitude);
transformRes.put(“lat”, latitude);
return transformRes;
}

/\*\*

* GCJ02转换BD09
*
* @param gcjLat GCL纬度坐标
* @param gcjLng GCL经度坐标
*/
public static Map<String, Double> Gcj02ToBd09(double gcjLat, double gcjLng) {
Map<String, Double> transformRes = new HashMap<>(2);
double z = Math.sqrt(gcjLng * gcjLng + gcjLat * gcjLat) + 0.00002 * Math.sin(gcjLat * PI);
double theta = Math.atan2(gcjLat, gcjLng) + 0.000003 * Math.cos(gcjLng * PI);
transformRes.put(“lon”, z * Math.cos(theta) + 0.0065);
transformRes.put(“lat”, z * Math.sin(theta) + 0.006);
return transformRes;
}

/\*\*

* BD09转换GCJ02
*
* @param bdLat 百度纬度坐标
* @param bdLng 百度经度坐标
*/
public static Map<String, Double> bd09ToGcj02(double bdLat, double bdLng) {
Map<String, Double> transformRes = new HashMap<>(2);
double x = bdLng - 0.0065, y = bdLat - 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);

    transformRes.put("lon", z \* Math.cos(theta));
    transformRes.put("lat", z \* Math.sin(theta));
    return transformRes;
}

/\*\*

* WGS84转换GCJ02优化(处理转换前经纬度坐标为null问题)
*
* @param fromLon 转换前的经度
* @param fromLat 转换前的纬度
* @param toLon 转换后的经度
* @param toLat 转换后的纬度
*/
private static void wgs84ToGcj02(Supplier fromLon, Supplier fromLat, Consumer toLon, Consumer toLat) {
wgs84ToGcj02(Optional.ofNullable(fromLon.get()).orElse(BigDecimal.ZERO), Optional.ofNullable(fromLat.get()).orElse(BigDecimal.ZERO), toLon, toLat);
}

/\*\*

* WGS84转换GCJ02优化(处理转换后经纬度坐标)
*
* @param fromLon 转换前的经度
* @param fromLat 转换前的纬度
* @param toLon 转换后的经度
* @param toLat 转换后的纬度
*/
private static void wgs84ToGcj02(BigDecimal fromLon, BigDecimal fromLat, Consumer toLon, Consumer toLat) {
final Map<String, Double> transformRes = wgs84ToGcj02(fromLon.doubleValue(), fromLat.doubleValue());
toLon.accept(new BigDecimal(transformRes.get(“lon”) + “”));
toLat.accept(new BigDecimal(transformRes.get(“lat”) + “”));
}

/\*\*

* 坐标是否在国外
*
* @param lon 经度
* @param lat 纬度
* @return true 国外坐标 false 国内坐标
*/
private static boolean outOfChina(double lon, double lat) {
if (lon < MIN_LON || lon > MAX_LON) {
return true;
}
return lat < MIN_LAT || lat > MAX_LAT;
}

/\*\*

* 转换经度坐标
*
* @param x 偏移后的经度
* @param y 偏移后的纬度
* @return double 转换经度坐标
*/
private static double transformLat(double x, double y) {
double transform = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
transform += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
transform += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
transform += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
return transform;
}

/\*\*

* 转换纬度坐标
*
* @param x 偏移后的经度
* @param y 偏移后的纬度
* @return double 转换纬度坐标
*/
private static double transformLon(double x, double y) {
double transform = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
transform += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
transform += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
transform += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
return transform;
}

/\*\*

* 坐标系转换工具类测试
*
* @param args 参数
*/
public static void main(String[] args) {

   // WGS84ToGCJ02 116.404, 39.915 转换后 [ 116.41024449916938, 39.91640428150164 ]
    double lon3 = 116.404D;
    double lat3 = 39.915D;
    Map<String, Double> transform3Result = wgs84ToGcj02(lon3, lat3);
    System.out.println("WGS84ToGCJ02 转换前经纬度(" + lon3 + "," + lat3 + ") 转换后经纬度(" + transform3Result.get("lon") + "," + transform3Result.get("lat") + ")");

    // GCJ02ToWGS84 116.404, 39.915 转换后 [ 116.39775550083061, 39.91359571849836 ]
    double lon4 = 116.404D;
    double lat4 = 39.915D;
    Map<String, Double> transform4Result = gcj02ToWgs84(lon4, lat4);
    System.out.println("GCJ02ToWGS84 转换前经纬度(" + lon4 + "," + lat4 + ") 转换后经纬度(" + transform4Result.get("lon") + "," + transform4Result.get("lat") + ")");

    // BD09ToGCJ02 116.404, 39.915 转换后 [ 116.39762729119315, 39.90865673957631 ]
    double lon5 = 116.404D;

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以点击这里获取

有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!**

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以点击这里获取

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值