Created
March 17, 2014 16:03
-
-
Save matiasinsaurralde/9602157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import( "fmt" | |
"math" ) | |
const x = math.Pi/180 | |
const R = 6371 | |
// robpike: https://twitter.com/mattfarina/status/344151151719104512 | |
func Rad(d float64) float64 { | |
return d*x | |
} | |
func Deg(r float64) float64 { | |
return r/x | |
} | |
func haversineDistance( lat1 float64, lon1 float64, lat2 float64, lon2 float64 ) float64 { | |
dLat := Rad((lat2-lat1)) | |
dLon := Rad((lon2-lon1)) | |
lat1 = Rad(lat1) | |
lat2 = Rad(lat2) | |
a := math.Sin(dLat/2) * math.Sin(dLat/2) + math.Sin(dLon/2) * math.Sin(dLon/2) * math.Cos(lat1) * math.Cos(lat2) | |
c := 2* math.Atan2(math.Sqrt(a), math.Sqrt(1-a)) | |
d := R * c | |
return d | |
} | |
func main() { | |
d := haversineDistance( -25.275890866265033, -57.51298248767853, -25.27522144836632, -57.515568137168884 ) | |
fmt.Println(d) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment