You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
532 B
18 lines
532 B
package tool
|
|
|
|
import "math"
|
|
|
|
const earthRadiusM = 6371000.0
|
|
|
|
// Haversine 计算两个经纬度坐标之间的球面距离(单位:米)
|
|
func Haversine(lat1, lon1, lat2, lon2 float64) float64 {
|
|
phi1 := lat1 * math.Pi / 180
|
|
phi2 := lat2 * math.Pi / 180
|
|
dPhi := (lat2 - lat1) * math.Pi / 180
|
|
dLambda := (lon2 - lon1) * math.Pi / 180
|
|
|
|
a := math.Sin(dPhi/2)*math.Sin(dPhi/2) +
|
|
math.Cos(phi1)*math.Cos(phi2)*math.Sin(dLambda/2)*math.Sin(dLambda/2)
|
|
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
|
|
return earthRadiusM * c
|
|
}
|
|
|