WGS ddmm.mmmm 与度 互相转化

1、搜索半天都是WEG转为度的,很少有度转为WGS的这里我完善了下

public class GpsToUtil {

	private static double pi = 3.14159265358979324;
	private static double a = 6378245.0;
	private static double ee = 0.00669342162296594323;
	private final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	/**
	 * WGS转2GCJ
	 * @param lat
	 * @param lon
	 * @return
	 * @Author : pjh. create at 2021年1月21日 上午10:42:42
	 */
	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;
	}
	    
	/**
	 * WGS先转为dd.dddd 再转为2gcj
	 * @param latStr  DDFF.FFFF
	 * @param lonStr  DDFF.FFFF
	 * @return   latStr   lonStr
	 * @Author : pjh. create at 2021年1月21日 上午10:42:57
	 */
	public static double[] wgs2gcj_dddddd(String latStr, String lonStr) {
        String wmm = latStr.substring(latStr.indexOf(".") - 2);
        String jmm = lonStr.substring(lonStr.indexOf(".") - 2);
        String wdd = latStr.substring(0, latStr.indexOf(".") - 2);
        String jdd = lonStr.substring(0, lonStr.indexOf(".") - 2);
        double wmm_a = Double.parseDouble(wmm) / 60;
        double jmm_a = Double.parseDouble(jmm) / 60;
        // 转成dd.dddd格式
        double lat = Double.parseDouble(wdd) + wmm_a;
        double lon = Double.parseDouble(jdd) + jmm_a;
        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;
	}
	    
	/**
	 * 高德转百度
	 * @param gd_lat
	 * @param gd_lon
	 * @return
	 * @Author : pjh. create at 2021年1月21日 上午10:43:30
	 */
	public static double[] gaoDeToBaidu(double gd_lat, double gd_lon) {
        double PI = 3.14159265358979324 * 3000.0 / 180.0;
        double x = gd_lon, y = gd_lat;
        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);
        double wgLat = z * Math.sin(theta) + 0.006;// 纬度
        double wgLon = z * Math.cos(theta) + 0.0065;// 经度
        double[] loc = { wgLat, wgLon };
        return loc;
    }

	/**
	 * 度转为   ddmm.mmmm
	 * @param angleInDegrees
	 * @return
	 */
	public static String degreesToDMSLon(double angleInDegrees) {
		// 转换为度、分钟
		int degrees = (int) angleInDegrees;
		double temp = (angleInDegrees - degrees) * 60 ;
		int minutes = (int) temp;
		double minutesPoint = (temp - minutes);
		minutesPoint = Math.round(minutesPoint * 10000);
		// 返回度分格式的字符串
		return String.format("%3d%02d.%04d", degrees, minutes, (int) Math.round(minutesPoint));
	}
	/**
	 * 度转为   ddmm.mmmm
	 * @param angleInDegrees
	 * @return
	 */
	public static String degreesToDMSLat(double angleInDegrees) {
		// 转换为度、分钟
		int degrees = (int) angleInDegrees;
		double temp = (angleInDegrees - degrees) * 60 ;
		int minutes = (int) temp;
		double minutesPoint = (temp - minutes);
		minutesPoint = Math.round(minutesPoint * 10000);
		// 返回度分格式的字符串
		return String.format("%2d%02d.%04d", degrees, minutes, (int) Math.round(minutesPoint));
	}
	/**
	 * WGS 转为 度
	 * @param dm
	 * @return
	 */
	public static double df2LatLng(String dm) {
		if (dm == null) {
			return 0;
		}
		try {
			dm = dm.replace(" ", "");
			int d = Integer.valueOf(dm.substring(0, dm.lastIndexOf(".") - 2));
			// 兼容经纬度的转换
			double fen = Double.parseDouble(dm.substring(String.valueOf(d).length()));

			double lat = (fen / 60) + Math.abs(d);
			if (lat < 0) {
				lat = -lat;
			}
			return Double.parseDouble(String.format("%.7f", lat));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public static void main(String[] args) {
        // 北斗芯片获取的经纬度为WGS84地理坐标ddmm.mmmm格式
        String w = "3004.1628";
        String j = "10403.5574";
		System.out.println( "WGS84数据:"+ df2LatLng(w) + ";" + df2LatLng(j));
		double[] a = wgs2gcj(df2LatLng(w), df2LatLng(j));
		System.out.println("WGS84转为高德数据:"+ a[0] +";" + a[1]);
	    double[] c = wgs2gcj_dddddd(w, j);
		System.out.println("WGS84转为高德数据:"+ c[0] +";" + c[1]);
		MapPoint p = PositionUtil.gcj_To_Gps84(c[1],c[0]);
		System.out.println("高德转为WGS84:"+ p.getLat() +";" + p.getLng());
		//dd.dddd 转为 ddmm.mmmm
		System.out.println("WGS原始数据:" + degreesToDMSLat(p.getLat()) + ";" + degreesToDMSLon(p.getLng()));
    }
}
public class PositionUtil {

    public static final String BAIDU_LBS_TYPE = "bd09ll";

    public static double pi = 3.1415926535897932384626;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    /**
     * 84 to 火星坐标系 (GCJ-02)
     *
     * @param lat
     * @param lon
     * @return
     */
    public static MapPoint gps84_To_Gcj02(double lon, double lat) {
        if (outOfChina(lon, lat)) {
            return null;
        }
        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 MapPoint(mgLon, mgLat);
    }

    /**
     * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
     * */
    public static MapPoint gcj_To_Gps84(double lon, double lat) {
        MapPoint gps = transform(lon, lat);
        double lontitude = lon * 2 - gps.getLng();
        double latitude = lat * 2 - gps.getLat();
        return new MapPoint(lontitude, latitude);
    }

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
     *
     * @param gg_lat
     * @param gg_lon
     */
    public static MapPoint gcj02_To_Bd09(double gg_lon, double gg_lat) {
        double x = gg_lon, y = gg_lat;
        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);
        double bd_lon = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        return new MapPoint(bd_lon, bd_lat);
    }

    /**
     * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
     * bd_lat * @param bd_lon * @return
     */
    public static MapPoint bd09_To_Gcj02(double bd_lon, double bd_lat) {
        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);
        double gg_lon = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new MapPoint(gg_lon, gg_lat);
    }

    /**
     * (BD-09)-->84
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    public static MapPoint bd09_To_Gps84(double bd_lon, double bd_lat) {
        MapPoint gcj02 = PositionUtil.bd09_To_Gcj02(bd_lat, bd_lon);
        MapPoint map84 = PositionUtil.gcj_To_Gps84(gcj02.getLat(),
                gcj02.getLng());
        return map84;
    }

    public static void main(String[] args) {
        MapPoint mapPoint = gps84_To_Gcj02(100.280295411,25.6047747599);
        System.out.println(JSONUtil.toJsonStr(mapPoint));
    }

    public static boolean outOfChina(double lon, double lat) {
        if (lon < 72.004 || lon > 137.8347) {
            return true;
        }
        if (lat < 0.8293 || lat > 55.8271) {
            return true;
        }
        return false;
    }

    public static MapPoint transform(double lon, double lat) {
        if (outOfChina(lon, lat)) {
            return new MapPoint(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 MapPoint(mgLon, mgLat);
    }

    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;
    }

}

运行效果(转化有精度丢失,不影响大局):

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

carry杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值