0%

安卓百度高德GPS坐标系转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69



//百度->gps
public static LatLng bdToWgs(LatLng bdLatLng) {
if (bdLatLng == null) return null;

// 百度坐标转高德坐标
double x = bdLatLng.longitude - 0.0065;
double y = bdLatLng.latitude - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);
double gcjLon = z * Math.cos(theta);
double gcjLat = z * Math.sin(theta);

// 高德坐标转 GPS
return gcjToWgs(new LatLng(gcjLat, gcjLon));
}

// GCJ-02 → WGS84
public static LatLng gcjToWgs(LatLng gcjLatLng) {
if (gcjLatLng == null) return null;
double dLat = transformLat(gcjLatLng.longitude - 105.0, gcjLatLng.latitude - 35.0);
double dLon = transformLon(gcjLatLng.longitude - 105.0, gcjLatLng.latitude - 35.0);
double radLat = gcjLatLng.latitude / 180.0 * Math.PI;
double magic = Math.sin(radLat);
magic = 1 - 0.00669342162296594323 * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((6378245.0 * (1 - 0.00669342162296594323)) / (magic * sqrtMagic) * Math.PI);
dLon = (dLon * 180.0) / (6378245.0 / sqrtMagic * Math.cos(radLat) * Math.PI);
double mgLat = gcjLatLng.latitude + dLat;
double mgLon = gcjLatLng.longitude + dLon;
return new LatLng(gcjLatLng.latitude * 2 - mgLat, gcjLatLng.longitude * 2 - mgLon);
}

private 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 * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
return ret;
}

private 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 * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
return ret;
}


//GPS-百度

public static LatLng wgsToBd(LatLng sourceLatLng) {
if (sourceLatLng == null) {
return null;
}

CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordinateConverter.CoordType.GPS);
converter.coord(sourceLatLng);

return converter.convert();
}