A friend of mine in Brisbane was wanting to know how far it is between two points on the globe. I have worked on this before, and had the code already. So here it is. It returns the distance between the points in meters.

p1 = Math.Cos(lat1 * 0.0174532925) * Math.Cos(lon1 * 0.0174532925) * Math.Cos(lat2 * 0.0174532925) * Math.Cos(lon2 * 0.0174532925)
p2 = Math.Cos(lat1 * 0.0174532925) * Math.Sin(lon1 * 0.0174532925) * Math.Cos(lat2 * 0.0174532925) * Math.Sin(lon2 * 0.0174532925)
p3 = Math.Sin(lat2 * 0.0174532925) * Math.Sin(lat1 * 0.0174532925)

temp = Math.Acos(p1 + p2 + p3) * 6378

It makes some assumptions about the shape of the earth – assuming that it is a sphere from memory. Since the equator bulges out a bit this equation does not hold perfectly, but it normally holds very very accurately – maybe 40m in 10,000km I would guess. In most cases this is not an issue. If it is then this is not the equation to use. And changing the multiplier will convert to statute miles or normal miles, or even meters.

For the use of working out which of a series of points is closest I cheat. I use pythagorus’ theorum to work out which is closest. This does have a minor error north/south, but it is so close when you only need to deal with relative distances.