Archivi tag: lat

How to get distance between two points

This chunk of code is taken form http://www.phpro.org/tutorials/Geo-Targetting-With-PHP-And-MySQL.html

The function below is really useful to calculate the distance between two points by latitude and longitude:

/**
 * Get distance from latitude and longitute
 *
 * @param float $lat_from
 * @param float $long_from
 * @param float $lat_to
 * @param float *long_to
 * @param $unit options k, m, n, Default k
 *
 * @return float
 */
function getRiemannDistance($lat_from, $long_from, $lat_to, $long_to, $unit='k'){
	switch ($unit){
		case 'm':	/* miles */
			$unit = 3963;
			break;
		case 'n':	/* nautical miles */
			$unit = 3444;
			break;
		default:	/* kilometers */
			$unit = 6371;
	}

	/*** 1 degree = 0.017453292519943 radius ***/
	$degreeRadius = deg2rad(1);

	/*** convert longitude and latitude to radians ***/
	$lat_from  *= $degreeRadius;
	$long_from *= $degreeRadius;
	$lat_to    *= $degreeRadius;
	$long_to   *= $degreeRadius;

	/*** apply the Great Circle Distance Formula ***/
	$dist = sin($lat_from) * sin($lat_to) + cos($lat_from)
	* cos($lat_to) * cos($long_from - $long_to);

	/*** radius of earth * arc cosine ***/
	return ($unit * acos($dist));
}
Annunci
Contrassegnato da tag , , , ,
%d blogger hanno fatto clic su Mi Piace per questo: