Created
May 18, 2016 11:00
-
-
Save aquaxp/a43e44672363896058235d0157394923 to your computer and use it in GitHub Desktop.
Haversine naive implementation
This file contains hidden or 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" | |
) | |
var ( | |
EARTH_RADIUS = 6378100.0 | |
) | |
func ToRad(angle float64) float64 { | |
return angle * (math.Pi / 180.0) | |
} | |
// Original Implementation from: http://www.movable-type.co.uk/scripts/latlong.html | |
func Distance(lat1, lon1, lat2, lon2 float64) float64 { | |
dLat := ToRad(lat2 - lat1) | |
dLon := ToRad(lon2 - lon1) | |
lat1Rad := ToRad(lat1) | |
lat2Rad := ToRad(lat2) | |
a1 := math.Sin(dLat/2) * math.Sin(dLat/2) | |
a2 := math.Cos(lat1Rad) * math.Cos(lat2Rad) * math.Sin(dLon/2) * math.Sin(dLon/2) | |
a := a1 + a2 | |
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a)) | |
return EARTH_RADIUS * c | |
} | |
func main() { | |
fmt.Println(Distance(55.028665, 82.918797, 55.056734, 82.923219)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment