邢飞龙

青青子衿,悠悠我心。

根据经纬度计算两地距离

最近项目需要个计算两地之间位置的功能,于是就找到了这个函数,纬度在前、经度在后。

<?php
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2)
{
	$theta = $longitude1 - $longitude2;
	$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
	$miles = acos($miles);
	$miles = rad2deg($miles);
	$miles = $miles * 60 * 1.1515;
	$feet = $miles * 5280;
	$yards = $feet / 3;
	$kilometers = $miles * 1.609344;
	$meters = $kilometers * 1000;
	return compact('miles', 'feet', 'yards', 'kilometers', 'meters');
}

$point1 = array( 
	'lat' => 39.92, 
	'long' => 116.46 
);
$point2 = array( 
	'lat' => 24.51, 
	'long' => 117.658 
);
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
print_r($distance);
?>
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注