Repository navigation
Expand file tree
/
Copy pathMapCoordinateHelper.cs
More file actions
168 lines (138 loc) · 6.32 KB
/
Copy pathMapCoordinateHelper.cs
File metadata and controls
168 lines (138 loc) · 6.32 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
namespace WPFDevelopers.Controls
{
public static class MapCoordinateHelper
{
private const double EarthRadius = 6378245.0;
private const double EE = 0.006693421622965943;
public static MapGeoPoint Wgs84ToGcj02(double lon, double lat)
{
var point = TransformWgs84ToGcj02(lon, lat);
return new MapGeoPoint(point.Item1, point.Item2, MapCoordinateType.Gcj02);
}
public static MapGeoPoint Gcj02ToWgs84(double lon, double lat)
{
var point = TransformGcj02ToWgs84(lon, lat);
return new MapGeoPoint(point.Item1, point.Item2, MapCoordinateType.Wgs84);
}
public static MapGeoPoint Wgs84ToBd09(double lon, double lat)
{
var gcj = Wgs84ToGcj02(lon, lat);
return Gcj02ToBd09(gcj.Longitude, gcj.Latitude);
}
public static MapGeoPoint Bd09ToWgs84(double lon, double lat)
{
var gcj = Bd09ToGcj02(lon, lat);
return Gcj02ToWgs84(gcj.Longitude, gcj.Latitude);
}
public static MapGeoPoint Gcj02ToBd09(double lon, double lat)
{
var x = lon;
var y = lat;
var z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * Math.PI);
var theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * Math.PI);
var bdLon = z * Math.Cos(theta) + 0.0065;
var bdLat = z * Math.Sin(theta) + 0.006;
return new MapGeoPoint(bdLon, bdLat, MapCoordinateType.Bd09);
}
public static MapGeoPoint Bd09ToGcj02(double lon, double lat)
{
var x = lon - 0.0065;
var y = lat - 0.006;
var z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * Math.PI);
var theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * Math.PI);
var gcjLon = z * Math.Cos(theta);
var gcjLat = z * Math.Sin(theta);
return new MapGeoPoint(gcjLon, gcjLat, MapCoordinateType.Gcj02);
}
public static MapGeoPoint Convert(MapGeoPoint point, MapCoordinateType targetType)
{
if (point == null)
{
throw new ArgumentNullException(nameof(point));
}
if (point.CoordinateType == targetType)
{
return new MapGeoPoint(point.Longitude, point.Latitude, targetType);
}
switch (point.CoordinateType)
{
case MapCoordinateType.Wgs84:
if (targetType == MapCoordinateType.Gcj02)
{
return Wgs84ToGcj02(point.Longitude, point.Latitude);
}
if (targetType == MapCoordinateType.Bd09)
{
return Wgs84ToBd09(point.Longitude, point.Latitude);
}
break;
case MapCoordinateType.Gcj02:
if (targetType == MapCoordinateType.Wgs84)
{
return Gcj02ToWgs84(point.Longitude, point.Latitude);
}
if (targetType == MapCoordinateType.Bd09)
{
return Gcj02ToBd09(point.Longitude, point.Latitude);
}
break;
case MapCoordinateType.Bd09:
if (targetType == MapCoordinateType.Wgs84)
{
return Bd09ToWgs84(point.Longitude, point.Latitude);
}
if (targetType == MapCoordinateType.Gcj02)
{
return Bd09ToGcj02(point.Longitude, point.Latitude);
}
break;
}
return new MapGeoPoint(point.Longitude, point.Latitude, targetType);
}
private static Tuple<double, double> TransformWgs84ToGcj02(double lon, double lat)
{
var dLat = TransformLat(lon - 105.0, lat - 35.0);
var dLon = TransformLon(lon - 105.0, lat - 35.0);
var radLat = lat * Math.PI / 180.0;
var magic = Math.Sin(radLat);
magic = 1 - EE * magic * magic;
var sqrtMagic = Math.Sqrt(magic);
dLat = (dLat * 180.0) / ((EarthRadius * (1 - EE)) / (sqrtMagic * 2) * Math.PI);
dLon = (dLon * 180.0) / ((EarthRadius * (1 - EE)) / (sqrtMagic * 2) * Math.PI);
var gcjLat = lat + dLat;
var gcjLon = lon + dLon;
return new Tuple<double, double>(gcjLon, gcjLat);
}
private static Tuple<double, double> TransformGcj02ToWgs84(double lon, double lat)
{
var dLat = TransformLat(lon - 105.0, lat - 35.0);
var dLon = TransformLon(lon - 105.0, lat - 35.0);
var radLat = lat * Math.PI / 180.0;
var magic = Math.Sin(radLat);
magic = 1 - EE * magic * magic;
var sqrtMagic = Math.Sqrt(magic);
dLat = (dLat * 180.0) / ((EarthRadius * (1 - EE)) / (sqrtMagic * 2) * Math.PI);
dLon = (dLon * 180.0) / ((EarthRadius * (1 - EE)) / (sqrtMagic * 2) * Math.PI);
var wgsLat = lat - dLat;
var wgsLon = lon - dLon;
return new Tuple<double, double>(wgsLon, wgsLat);
}
private static double TransformLat(double x, double y)
{
var 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.0 * Math.Sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double TransformLon(double x, double y)
{
var 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 * Math.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
}
}