-
Notifications
You must be signed in to change notification settings - Fork 13
/
geo.go
33 lines (26 loc) · 876 Bytes
/
geo.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
package helper
import (
"math"
)
type Coordinates struct {
Latitude float64
Longitude float64
}
const radius = 6371 // Earth's mean radius in kilometers
func degrees2radians(degrees float64) float64 {
return degrees * math.Pi / 180
}
func (origin Coordinates) DistanceInKm(destination Coordinates) float64 {
degreesLat := degrees2radians(destination.Latitude - origin.Latitude)
degreesLong := degrees2radians(destination.Longitude - origin.Longitude)
a := math.Sin(degreesLat/2)*math.Sin(degreesLat/2) +
math.Cos(degrees2radians(origin.Latitude))*
math.Cos(degrees2radians(destination.Latitude))*math.Sin(degreesLong/2)*
math.Sin(degreesLong/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
d := radius * c
return d
}
func (origin Coordinates) DistanceInMeters(destination Coordinates) float64 {
return origin.DistanceInKm(destination) * 1000
}