solidarity-service/utils/utils.go

23 lines
495 B
Go
Raw Normal View History

2023-10-20 11:41:39 +00:00
package utils
import "math"
func Haversine(lat1, lon1, lat2, lon2 float64) float64 {
// Radius of the Earth in kilometers
R := 6371.0
lat1 = lat1 * math.Pi / 180
lon1 = lon1 * math.Pi / 180
lat2 = lat2 * math.Pi / 180
lon2 = lon2 * math.Pi / 180
dlat := lat2 - lat1
dlon := lon2 - lon1
a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Cos(lat1)*math.Cos(lat2)*math.Sin(dlon/2)*math.Sin(dlon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
distance := R * c
return distance
}