Haversine distance

Haversine distance is the great-circle distance between two points on a sphere, calculated from their latitudes and longitudes using trigonometric functions. It provides an approximation of the shortest surface distance between geographic coordinates. The Go and SQL implementations must use the same Earth radius, 6371 kilometres, to produce consistent results.

Minimal example

go
a := math.Sin(dLat/2)*math.Sin(dLat/2) +
	math.Cos(lat1)*math.Cos(lat2)*math.Sin(dLon/2)*math.Sin(dLon/2)
distance := 2 * 6371 * math.Asin(math.Sqrt(a))

Learn this concept

Lesson 2.25 · Haversine distance and geo indexes

Used in exercises

Exercise 2.26 · GET /v1/restaurants: sort by distance Exercise 5.17 · Rider map geometry library