<?php

class Geo
{

	static function distance($latlng1, $latlng2)
	{

		// Convert degrees to radians.
		$lat1 = deg2rad($latlng1['lat']);
		$lng1 = deg2rad($latlng1['lng']);
		$lat2 = deg2rad($latlng2['lat']);
		$lng2 = deg2rad($latlng2['lng']);

		// Calculate delta longitude and latitude.
		$delta_lat = ($lat2 - $lat1);
		$delta_lng = ($lng2 - $lng1);

		$distance = (6378137 * acos(cos($lat1) * cos($lat2) * cos($lng1 - $lng2) + sin($lat1) * sin($lat2)));

		if (isset($latlng1['alt']) && isset($latlng2['alt']) && $latlng1['alt'] > 0 && $latlng1['alt'] <> $latlng2['alt']) {
			$altDelta = abs($latlng1['alt'] - $latlng2['alt']);
			$distance = sqrt(pow($distance, 2) + pow($altDelta, 2));
		}

		return $distance;
	}

	static function formatedDistance($distance)
	{
		return $distance > 1000 ? round($distance/1000, 2).'км' : round($distance).'м';
	}
}
