WGS84 地球坐标系,GCJ02 火星坐标系,BD09 百度坐标系简介与转换,java 项目视频百度云盘
2.2 各坐标系转换工具类
package com.jourwon.util;
import com.jourwon.pojo.Point;
/**
Description: 各坐标系之间的转换工具类
@author JourWon
@date Created on 2018 年 6 月 19 日
*/
public class CoordinateTransformUtils {
// 圆周率π
private static final double PI = 3.1415926535897932384626D;
// 火星坐标系与百度坐标系转换的中间量
private static final double X_PI = 3.14159265358979324 * 3000.0 / 180.0D;
// Krasovsky 1940
// 长半轴 a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// 扁率 ee = (a^2 - b^2) / a^2;
// 长半轴
private static final double SEMI_MAJOR = 6378245.0D;
// 扁率
private static final double FLATTENING = 0.00669342162296594323D;
// WGS84=>GCJ02 地球坐标系=>火星坐标系
public static Point wgs84ToGcj02(double lng, double lat) {
if (outOfChina(lng, lat)) {
return new Point(lng, lat);
}
double[] offset = offset(lng, lat);
double mglng = lng + offset[0];
double mglat = lat + offset[1];
return new Point(mglng, mglat);
}
// GCJ02=>WGS84 火星坐标系=>地球坐标系(粗略)
public static Point gcj02ToWgs84(double lng, double lat) {
if (outOfChina(lng, lat)) {
return new Point(lng, lat);
}
double[] offset = offset(lng, lat);
double mglng = lng - offset[0];
double mglat = lat - offset[1];
return new Point(mglng, mglat);
}
// GCJ02=>WGS84 火星坐标系=>地球坐标系(精确)
public static Point gcj02ToWgs84Exactly(double lng, double lat) {
if (outOfChina(lng, lat)) {
return new Point(lng, lat);
}
double initDelta = 0.01;
double threshold = 0.000000001;
double dLat = initDelta, dLon = initDelta;
double mLat = lat - dLat, mLon = lng - dLon;
double pLat = lat + dLat, pLon = lng + dLon;
double wgsLat, wgsLng, i = 0;
while (true) {
wgsLat = (mLat + pLat) / 2;
wgsLng = (mLon + pLon) / 2;
Point point = wgs84ToGcj02(wgsLng, wgsLat);
dLon = point.getLng() - lng;
dLat = point.getLat() - lat;
if ((Math.abs(dLat) < threshold) && (Math.abs(dLon) < threshold))
break;
if (dLat > 0)
pLat = wgsLat;
else
mLat = wgsLat;
if (dLon > 0)
pLon = wgsLng;
else
mLon = wgsLng;
if (++i > 10000)
break;
}
return new Point(wgsLng, wgsLat);
}
// GCJ-02=>BD09 火星坐标系=>百度坐标系
public static Point gcj02ToBd09(double lng, double lat) {
double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * X_PI);
double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * X_PI);
double bd_lng = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) +
《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
0.006;
return new Point(bd_lng, bd_lat);
}
// BD09=>GCJ-02 百度坐标系=>火星坐标系
public static Point bd09ToGcj02(double lng, double lat) {
double x = lng - 0.0065;
double 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 gcj_lng = z * Math.cos(theta);
double gcj_lat = z * Math.sin(theta);
return new Point(gcj_lng, gcj_lat);
}
// WGS84=>BD09 地球坐标系=>百度坐标系
public static Point wgs84ToBd09(double lng, double lat) {
Point point = wgs84ToGcj02(lng, lat);
return gcj02ToBd09(point.getLng(), point.getLat());
}
// BD09=>WGS84 百度坐标系=>地球坐标系
public static Point bd09ToWgs84(double lng, double lat) {
Point point = bd09ToGcj02(lng, lat);
return gcj02ToWgs84(point.getLng(), point.getLat());
}
/**
Description: 中国境外返回 true,境内返回 false
@param lng 经度
@param lat 纬度
@return
*/
public static boolean outOfChina(double lng, double lat) {
if (lng < 72.004 || lng > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
// 经度偏移量
private static double transformLng(double lng, double lat) {
double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
return ret;
}
// 纬度偏移量
private static double transformLat(double lng, double lat) {
double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat
0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * 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 += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
return ret;
}
// 偏移量
public static double[] offset(double lng, double lat) {
double[] lngLat = new double[2];
double dlng = transformLng(lng - 105.0, lat - 35.0);
double dlat = transformLat(lng - 105.0, lat - 35.0);
double radlat = lat / 180.0 * PI;
double magic = Math.sin(radlat);
magic = 1 - FLATTENING * magic * magic;
double sqrtmagic = Math.sqrt(magic);
dlng = (dlng * 180.0) / (SEMI_MAJOR / sqrtmagic * Math.cos(radlat) * PI);
dlat = (dlat * 180.0) / ((SEMI_MAJOR * (1 - FLATTENING)) / (magic * sqrtmagic) * PI);
lngLat[0] = dlng;
lngLat[1] = dlat;
return lngLat;
}
}
/**
Description: 坐标系转换工具测试类
@author JourWon
@date Created on 2018 年 6 月 19 日
*/
public class CoordinateTransformUtilsTest {
/**
Description: 地球坐标系 =>火星坐标系、百度坐标系
*/
@Test
public void test01() {
// 广州市中大地铁站
Point point = new Point(113.28749670783887D, 23.094783676708065D);
System.out.println("地球坐标系 : " + point);
Point wgs84ToGcj02 = CoordinateTransformUtils.wgs84ToGcj02(point.getLng(), point.getLat());
System.out.println("火星坐标系 : " + wgs84ToGcj02);
Point wgs84ToBd09 = CoordinateTransformUtils.wgs84ToBd09(point.getLng(), point.getLat());
System.out.println("百度坐标系 : " + wgs84ToBd09);
}
/**
Description: 火星坐标系=>地球坐标系、百度坐标系
*/
@Test
public void test02() {
最后
Java 架构进阶面试及知识点文档笔记
这份文档共 498 页,其中包括 Java 集合,并发编程,JVM,Dubbo,Redis,Spring 全家桶,MySQL,Kafka 等面试解析及知识点整理

Java 分布式高级面试问题解析文档
其中都是包括分布式的面试问题解析,内容有分布式消息队列,Redis 缓存,分库分表,微服务架构,分布式高可用,读写分离等等!

互联网 Java 程序员面试必备问题解析及文档学习笔记

Java 架构进阶视频解析合集
评论