1 public class Gps
2 {
3
4 private double wgLat;
5 private double wgLon;
6
7 public Gps(double wgLat, double wgLon)
8 {
9 setWgLat(wgLat);
10 setWgLon(wgLon);
11 }
12
13 public double getWgLat()
14 {
15 return wgLat;
16 }
17
18 public void setWgLat(double wgLat)
19 {
20 this.wgLat = wgLat;
21 }
22
23 public double getWgLon()
24 {
25 return wgLon;
26 }
27
28 public void setWgLon(double wgLon)
29 {
30 this.wgLon = wgLon;
31 }
32
33 public String toString()
34 {
35 return wgLat + "," + wgLon;
36 }
37 }
1 public class PositionUtil {
2
3 public static String BAIDU_LBS_TYPE = "bd09ll";
4
5 public static double pi = 3.1415926535897932384626;
6 public static double a = 6378245.0;
7 public static double ee = 0.00669342162296594323;
8
9 /**
10 * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
11 *
12 * @param lat
13 * @param lon
14 * @return
15 */
16 public static Gps gps84_To_Gcj02(double lat, double lon) {
17 if (outOfChina(lat, lon)) {
18 return null;
19 }
20 double dLat = transformLat(lon - 105.0, lat - 35.0);
21 double dLon = transformLon(lon - 105.0, lat - 35.0);
22 double radLat = lat / 180.0 * pi;
23 double magic = Math.Sin(radLat);
24 magic = 1 - ee * magic * magic;
25 double sqrtMagic = Math.Sqrt(magic);
26 dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
27 dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
28 double mgLat = lat + dLat;
29 double mgLon = lon + dLon;
30 return new Gps(mgLat, mgLon);
31 }
32
33 /**
34 * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
35 * */
36 public static Gps gcj_To_Gps84(double lat, double lon) {
37 Gps gps = transform(lat, lon);
38 double lontitude = lon * 2 - gps.getWgLon();
39 double latitude = lat * 2 - gps.getWgLat();
40 return new Gps(latitude, lontitude);
41 }
42
43 /**
44 * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
45 *
46 * @param gg_lat
47 * @param gg_lon
48 */
49 public static Gps gcj02_To_Bd09(double gg_lat, double gg_lon) {
50 double x = gg_lon, y = gg_lat;
51 double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * pi);
52 double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * pi);
53 double bd_lon = z * Math.Cos(theta) + 0.0065;
54 double bd_lat = z * Math.Sin(theta) + 0.006;
55 return new Gps(bd_lat, bd_lon);
56 }
57
58 /**
59 * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
60 * bd_lat * @param bd_lon * @return
61 */
62 public static Gps bd09_To_Gcj02(double bd_lat, double bd_lon) {
63 double x = bd_lon - 0.0065, y = bd_lat - 0.006;
64 double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * pi);
65 double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * pi);
66 double gg_lon = z * Math.Cos(theta);
67 double gg_lat = z * Math.Sin(theta);
68 return new Gps(gg_lat, gg_lon);
69 }
70
71 /**
72 * (BD-09)-->84
73 * @param bd_lat
74 * @param bd_lon
75 * @return
76 */
77 public static Gps bd09_To_Gps84(double bd_lat, double bd_lon) {
78
79 Gps gcj02 = PositionUtil.bd09_To_Gcj02(bd_lat, bd_lon);
80 Gps map84 = PositionUtil.gcj_To_Gps84(gcj02.getWgLat(),
81 gcj02.getWgLon());
82 return map84;
83
84 }
85
86 public static bool outOfChina(double lat, double lon) {
87 if (lon < 72.004 || lon > 137.8347)
88 return true;
89 if (lat < 0.8293 || lat > 55.8271)
90 return true;
91 return false;
92 }
93
94 public static Gps transform(double lat, double lon) {
95 if (outOfChina(lat, lon)) {
96 return new Gps(lat, lon);
97 }
98 double dLat = transformLat(lon - 105.0, lat - 35.0);
99 double dLon = transformLon(lon - 105.0, lat - 35.0);
100 double radLat = lat / 180.0 * pi;
101 double magic = Math.Sin(radLat);
102 magic = 1 - ee * magic * magic;
103 double sqrtMagic = Math.Sqrt(magic);
104 dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
105 dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
106 double mgLat = lat + dLat;
107 double mgLon = lon + dLon;
108 return new Gps(mgLat, mgLon);
109 }
110
111 public static double transformLat(double x, double y) {
112 double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
113 + 0.2 * Math.Sqrt(Math.Abs(x));
114 ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
115 ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
116 ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
117 return ret;
118 }
119
120 public static double transformLon(double x, double y) {
121 double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
122 * Math.Sqrt(Math.Abs(x));
123 ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
124 ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
125 ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0
126 * pi)) * 2.0 / 3.0;
127 return ret;
128 }
129
130 }