ref: f56066c28fc1ce985d2ac984664b9b02d7a9534b
server/geo/distances.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 |
package geo import "math" type coordinate struct { lat float64 lon float64 } // haversin(θ) function func hsin(theta float64) float64 { return math.Pow(math.Sin(theta/2), 2) } func Distance(coordinate1, coordinate2 coordinate) float64 { var la1, lo1, la2, lo2, r float64 la1 = coordinate1.lat * math.Pi / 180 lo1 = coordinate1.lon * math.Pi / 180 la2 = coordinate2.lat * math.Pi / 180 lo2 = coordinate2.lon * math.Pi / 180 r = 6378100 // Earth radius // calculate h := hsin(la2-la1) + math.Cos(la1)*math.Cos(la2)*hsin(lo2-lo1) return 2 * r * math.Asin(math.Sqrt(h)) } |