This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gps.go
126 lines (107 loc) · 3.79 KB
/
gps.go
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
package main
import "math"
const EARTH_RADIUS float64 = 6370.856 //km 地球半径 平均值,千米
//地球半径
const EARTH_R float64 = 6378245.0
func HaverSin(theta float64) float64 {
v := math.Sin(theta / 2)
return v * v
}
func Distance(lat1, lon1, lat2, lon2 float64) float64 {
//用haversine公式计算球面两点间的距离。
//经纬度转换成弧度
lat1 = ConvertDegreesToRadians(lat1)
lon1 = ConvertDegreesToRadians(lon1)
lat2 = ConvertDegreesToRadians(lat2)
lon2 = ConvertDegreesToRadians(lon2)
//差值
var vLon float64 = math.Abs(lon1 - lon2)
var vLat float64 = math.Abs(lat1 - lat2)
//h is the great circle distance in radians, great circle就是一个球体上的切面,它的圆心即是球心的一个周长最大的圆。
var h float64 = HaverSin(vLat) + math.Cos(lat1)*math.Cos(lat2)*HaverSin(vLon)
var distance float64 = 2 * EARTH_RADIUS * math.Asin(math.Sqrt(h))
return distance
}
/**
* 计算两点之间的角度
*
* @param pntFirst
* @param pntNext
* @return
*/
func GetAngle(lat1, lon1, lat2, lon2 float64) float64 {
var dRotateAngle float64 = math.Atan2(math.Abs(lon1-lon2), math.Abs(lat1-lat2))
if lon2 >= lon1 {
if lat2 >= lat1 {
} else {
dRotateAngle = math.Pi - dRotateAngle
}
} else {
if lat2 >= lat1 {
dRotateAngle = 2*math.Pi - dRotateAngle
} else {
dRotateAngle = math.Pi + dRotateAngle
}
}
dRotateAngle = dRotateAngle * 180 / math.Pi
return dRotateAngle
}
func ConvertDegreesToRadians(degrees float64) float64 {
return degrees * math.Pi / 180.0
}
func ConvertRadiansToDegrees(radian float64) float64 {
return radian * 180.0 / math.Pi
}
const ee float64 = 0.00669342162296594323
const XPI float64 = math.Pi * 3000.0 / 180.0
//世界大地坐标转为百度坐标
func wgs2bd(lat, lon float64) (bdlat, bdlng float64) {
gcjlat, gcjlng := wgs2gcj(lat, lon)
bdlat, bdlng = gcj2bd(gcjlat, gcjlng)
return
}
func gcj2bd(lat, lon float64) (bdlat, bdlng float64) {
var x float64 = lon
var y float64 = lat
var z float64 = math.Sqrt(x*x+y*y) + 0.00002*math.Sin(y*XPI)
var theta float64 = math.Atan2(y, x) + 0.000003*math.Cos(x*XPI)
bdlng = z*math.Cos(theta) + 0.0065
bdlat = z*math.Sin(theta) + 0.006
return
}
func bd2gcj(lat, lon float64) (gcjlat, gcjlng float64) {
var x float64 = lon - 0.0065
var y float64 = lat - 0.006
var z float64 = math.Sqrt(x*x+y*y) - 0.00002*math.Sin(y*XPI)
var theta float64 = math.Atan2(y, x) - 0.000003*math.Cos(x*XPI)
gcjlng = z * math.Cos(theta)
gcjlat = z * math.Sin(theta)
return
}
func wgs2gcj(lat, lon float64) (gcjlat, gcjlng float64) {
var dLat float64 = transformLat(lon-105.0, lat-35.0)
var dLon float64 = transformLon(lon-105.0, lat-35.0)
var radLat float64 = lat / 180.0 * math.Pi
var magic float64 = math.Sin(radLat)
magic = 1 - ee*magic*magic
var sqrtMagic float64 = math.Sqrt(magic)
dLat = (dLat * 180.0) / ((EARTH_R * (1 - ee)) / (magic * sqrtMagic) * math.Pi)
dLon = (dLon * 180.0) / (EARTH_R / sqrtMagic * math.Cos(radLat) * math.Pi)
gcjlat = lat + dLat
gcjlng = lon + dLon
return
}
func transformLat(lat, lon float64) float64 {
var 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*math.Pi) + 20.0*math.Sin(2.0*lat*math.Pi)) * 2.0 / 3.0
ret += (20.0*math.Sin(lon*math.Pi) + 40.0*math.Sin(lon/3.0*math.Pi)) * 2.0 / 3.0
ret += (160.0*math.Sin(lon/12.0*math.Pi) + 320*math.Sin(lon*math.Pi/30.0)) * 2.0 / 3.0
return ret
}
func transformLon(lat, lon float64) float64 {
var 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*math.Pi) + 20.0*math.Sin(2.0*lat*math.Pi)) * 2.0 / 3.0
ret += (20.0*math.Sin(lat*math.Pi) + 40.0*math.Sin(lat/3.0*math.Pi)) * 2.0 / 3.0
ret += (150.0*math.Sin(lat/12.0*math.Pi) + 300.0*math.Sin(lat/30.0*math.Pi)) * 2.0 / 3.0
return ret
}