Skip to content

Instantly share code, notes, and snippets.

@kuba--
Created June 6, 2017 22:31
Show Gist options
  • Save kuba--/78bebdcb33e558294f6370821b13a551 to your computer and use it in GitHub Desktop.
Save kuba--/78bebdcb33e558294f6370821b13a551 to your computer and use it in GitHub Desktop.
Haversine formula
func dist(lat1, lon1, lat2, lon2 float64) float64 {
const R = 1000.0 * 6378.132 // Radius of earth in m.
lat := lat2*math.Pi/180.0 - lat1*math.Pi/180.0
lon := lon2*math.Pi/180.0 - lon1*math.Pi/180.0
a := math.Sin(lat/2.0)*math.Sin(lat/2.0) + math.Cos(lat1*math.Pi/180.0)*math.Cos(lat2*math.Pi/180.0)*math.Sin(lon/2.0)*math.Sin(lon/2.0)
c := 2.0 * math.Atan2(math.Sqrt(a), math.Sqrt(1.0-a))
d := R * c
return d
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment