1.分不清的坐标系
WG-S84: 地理坐标系统,GPS仪器记录的经纬度信息,Google Earth采用,Google Map中国范围外使用,高德地图中国范围外使用。 GCJ-02: 投影坐标系统,火星坐标系,中国国家测绘局制定的坐标系统,由WGS-84加密后的坐标。Google中国和搜搜地图,arcgis地图,高德地图 BD-09: 投影坐标系统,百度坐标,GCJ-02加密后的坐标系,只适用于百度地图
(在国内是不允许直接用WGS84坐标系标注的,必须经过加密后才能用。必须至少使用GCJ-02坐标系,或者使用在GCJ-02加密后再进行加密的坐标系,如百度坐标系)
其他:搜狗地图:搜狗坐标系,图吧:图吧坐标等,估计也是在GCJ02基础上加密而成的,这里暂不涉及
2.直接上代码
package ft.util.gps;// 导入Proj4J库import org.locationtech.proj4j.*;import org.locationtech.proj4j.io.Proj4FileReader;import java.io.IOException;public class CoordinateTransformUtil { static double x_pi = 3.14159265358979324 * 3000.0 / 180.0; // π static double pi = 3.1415926535897932384626; // 长半轴 static double a = 6378245.0; // 扁率 static double ee = 0.00669342162296594323; public static PointXY bd09towgs84(double lng, double lat) { PointXY gcj = bd09togcj02(lng, lat); PointXY wgs84 = gcj02towgs84(gcj.getLon(), gcj.getLat()); return wgs84; } public static PointXY wgs84tobd09(double lng, double lat) { PointXY gcj = wgs84togcj02(lng, lat); PointXY bd09 = gcj02tobd09(gcj.getLon(), gcj.getLat()); return bd09; } public static PointXY 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) + 0.006; return new PointXY(bd_lng, bd_lat); } public static PointXY 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_lng = z * Math.cos(theta); double gg_lat = z * Math.sin(theta); return new PointXY(gg_lng, gg_lat); } public static PointXY wgs84togcj02(double lng, double lat) { if (out_of_china(lng, lat)) { return new PointXY(lng, lat); } double dlat = transformlat(lng - 105.0, lat - 35.0); double dlng = transformlng(lng - 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); dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi); double mglat = lat + dlat; double mglng = lng + dlng; return new PointXY(mglng, mglat); } public static PointXY gcj02towgs84(double lng, double lat) { if (out_of_china(lng, lat)) { return new PointXY(lng, lat); } double dlat = transformlat(lng - 105.0, lat - 35.0); double dlng = transformlng(lng - 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); dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi); double mglat = lat + dlat; double mglng = lng + dlng; return new PointXY(lng * 2 - mglng, lat * 2 - mglat); } public 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 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; } public static boolean out_of_china(double lng, double lat) { if (lng < 72.004 || lng > 137.8347) { return true; } else if (lat < 0.8293 || lat > 55.8271) { return true; } return false; } public static PointXY wgs2cgcg(double lon, double lat) { Proj4FileReader proj4FileReader = new Proj4FileReader(); String[] paramStr = new String[0]; try { paramStr = proj4FileReader.readParametersFromFile("epsg","4528"); } catch (IOException e) { e.printStackTrace(); } CRSFactory targetFactory = new CRSFactory(); //目标坐标系统 CoordinateReferenceSystem cgcs = targetFactory.createFromParameters("4528", paramStr); //源坐标系统 CRSFactory crsFactory = new CRSFactory(); String wgs84_param = "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degress"; CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84_param); CoordinateTransformFactory ctf = new CoordinateTransformFactory(); CoordinateTransform transform = ctf.createTransform(wgs84, cgcs); ProjCoordinate projCoordinate = new ProjCoordinate(lon,lat); transform.transform(projCoordinate,projCoordinate); return new PointXY(projCoordinate.x,projCoordinate.y); } public static PointXY cgcg2wgs(double lon, double lat) { Proj4FileReader proj4FileReader = new Proj4FileReader(); String[] paramStr = new String[0]; try { paramStr = proj4FileReader.readParametersFromFile("epsg","4528"); } catch (IOException e) { e.printStackTrace(); } CRSFactory targetFactory = new CRSFactory(); //目标坐标系统 CoordinateReferenceSystem cgcs = targetFactory.createFromParameters("4528", paramStr); //源坐标系统 CRSFactory crsFactory = new CRSFactory(); String wgs84_param = "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degress"; CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84_param); CoordinateTransformFactory ctf = new CoordinateTransformFactory(); CoordinateTransform transform = ctf.createTransform(cgcs,wgs84); ProjCoordinate projCoordinate = new ProjCoordinate(lon,lat); transform.transform(projCoordinate,projCoordinate); return new PointXY(projCoordinate.x,projCoordinate.y); } public static void main(String[] args) { // 坐标转化api // https://tool.lu/coordinate/?ivk_sa=1024320u // System.out.println(wgs84togcj02(118.840344,31.99335)); // ok System.out.println(gcj02towgs84(107.80473041151717, 29.33190938671697)); // ok System.out.println(gcj02tobd09(107.80473041151717, 29.33190938671697)); // ok System.out.println(wgs84tobd09(107.8003647, 29.3347596)); // ok System.out.println(bd09togcj02(107.81129990277971, 29.337686168994296)); System.out.println(bd09towgs84(107.81129990277971, 29.337686168994296)); System.out.println(wgs2cgcg(118.70331743312532,32.103750473727125));// System.out.println(bd09towgs84(120.24794,29.72979));//gcj02towgs84// System.out.println(gcj02tobd09(120.236701, 29.726141));// PointXY p = wgs2cgcg(118.70331743312532,32.103750473727125); System.out.println(p); System.out.println(cgcg2wgs(p.getLon(), p.getLat())); }}`在这里插入代码片`
测试结果
lon:118.84543348019479,lat:31.99121178083339
lon:107.80037405167121,lat:29.334768526991336
lon:107.81129968211874,lat:29.337686107444082
lon:107.81129968211874,lat:29.337686107444082
lon:107.80473019794286,lat:29.33191023473475
lon:107.80037383758244,lat:29.33476937412017
lon:4.0377605981347054E7,lat:3554093.2140468676
lon:4.0377605981347054E7,lat:3554093.2140468676
lon:118.70331743312477,lat:32.10375047372694
来源地址:https://blog.csdn.net/u011024436/article/details/133745789