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] * @param float $earthRadius Mean earth radius in [m] * @return float Distance between points in [m] (same as earthRadius) */ function vincentyGreatCircleDistance( $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) { // convert from degrees to radians $latFrom = deg2rad($latitudeFrom); $lonFrom = deg2rad($longitudeFrom); $latTo = deg2rad($latitudeTo); $lonTo = deg2rad($longitudeTo); $lonDelta = $lonTo - $lonFrom; $a = pow(cos($latTo) * sin($lonDelta), 2) + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); $angle = atan2(sqrt($a), $b); return $angle * $earthRadius; }