Package haversine provides the great circle distance between two points on the surface of the earth. Points are identified by latitude and longitude, and distance results can be returned in nautical miles, statute miles, or kilometers.
The curvature of the earth dictates that the shortest distance between two points cannot be a straight line between the points. The Haversine formula provides an approximation for the shortest great circle route over the surface of earth that connects the two points. In this figure the dotted yellow line is the arc of a great circle. Image courtesy USGS.
go get github.com/FATHOM5/haversine
The example below shows how far I'd have to walk in my boots to go visit my co-founder.
package main
import (
"fmt"
"github.com/FATHOM5/haversine"
)
func main() {
austin := haversine.Coord{ // Austin, Texas
Lat: 30.2672,
Lon: -97.7431,
}
paloAlto := haversine.Coord{ // Palo Alto, California
Lat: 37.4419,
Lon: -122.1430,
}
nm := haversine.Distance(austin, paloAlto)
fmt.Printf("%.1f miles is a long walk to Silicon Valley.\n", nm)
// Output: 1286.1 miles is a long walk to Silicon Valley.
}
- https://plus.maths.org/content/lost-lovely-haversine
- https://en.wikipedia.org/wiki/Haversine_formula
- Many thanks to the original author, umahmood, for a great package to begin this fork.
See LICENSE for rights and limitations (MIT).