# Calculating distance between longitude and latitude pairs

Problem: I need a simple way to calculate distance between two pairs of longitude and latitude coordinates.

Apparently there are several ways of making this calculation.  Since none of my calculations would exceed 15 miles I was able to use the flat earth calculation which is the simplest but inaccurate as distances increase between the two points according to this wikipedia article.  Here’s a blurb about the flat-earth formula:

## Flat-surface formula

A planar approximation for the surface of the earth may be useful over small distances. The accuracy of distance calculations using this approximation become increasingly inaccurate as:

• The separation between the points becomes greater;
• A point becomes closer to a geographic pole.

There were a bunch of sites that came up with all the different formulae over this but I’m not launching a satellite or programming a guided missile. The following code came from perlmonks which is a very reputable reference for anything to do with programming in perl. Here is the subroutine I chose to use. Article was written in 2002.

``` use Math::Trig sub FlatEarth { my (\$lat1, \$long1, \$lat2, \$long2) = @_; my \$r=3956; my \$a = (pi/2)- deg2rad(\$lat1); my \$b = (pi/2)- deg2rad(\$lat2) my \$c = sqrt(\$a**2 + \$b**2 - 2 * \$a *\$b *cos(deg2rad(\$long2)-deg2rad(\$long1))); my \$dist = \$c * \$r; return \$dist; }```

The above code seems to work.  Most of the calculations I needed to do were under a mile.