 # Calculating distance between two GPS coordinates

Now that I have a PHP script that can find out my physical location, I wanted to calculate how far from home I am. Since ‘home’ is a fixed GPS coordinate and my actual position varies, I needed a method to calculate the distance between two GPS coordinates. I found this suggested PHP code for the purpose and tested it out, it works pretty neat. I have updated my PHP script now to calculate the distance in meters between my current location and my home, it outputs something like this now:

```Current location is:42.****,23.*****, accuracy is 40 m (Sofia, Bulgaria) from 12/18/12 08:13:31
Distance from home:19994.1161466m```

(I have masked my actual location for privacy purposes)

So the PHP script now not only knows whre I am, but how far I am, may come in handy in my future home automation improvements.

```/**
* Calculates the great-circle distance between two points, with
* the Vincenty formula.
* @param float \$latitudeFrom Latitude of start point in [deg decimal]
* @param float \$longitudeFrom Longitude of start point in [deg decimal]
* @param float \$latitudeTo Latitude of target point in [deg decimal]
* @param float \$longitudeTo Longitude of target point in [deg decimal]
* @return float Distance between points in [m] (same as earthRadius)
*/
function vincentyGreatCircleDistance(
\$latitudeFrom, \$longitudeFrom, \$latitudeTo, \$longitudeTo, \$earthRadius = 6371000)
{
// convert from degrees to radians