在涉及到包含经纬度的项目时通常需要计算两个坐标之间的距离
/**
* 计算两个坐标之间的距离(米)
* @param [array] $from [起点坐标(经纬度)]
* @param [array] $to [终点坐标(经纬度)]
* @return int
*/
private function getDistance($from,$to)
{
sort($from);
sort($to);
$radius=6371393;
$lat1=($from[0]* pi()) / 180;
$lng1=($from[1]* pi()) / 180;
$lat2=($to[0]* pi()) / 180;
$lng2=($to[1]* pi()) / 180;
$stepOne= pow(sin(($lat2-$lat1)/2),2) + cos($lat1) * cos($lat2) * pow(sin(($lng2-$lng1)/2),2);
$stepTwo=2* asin(min(1, sqrt($stepOne)));
return round($radius*$stepTwo);
}
登录后可发表评论