温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用php怎么对两坐标点之间的距离进行计算

发布时间:2020-12-28 15:59:31 来源:亿速云 阅读:229 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用php怎么对两坐标点之间的距离进行计算,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

当比较近的时候,可以忽略球面因素,当做是一个平面,这样就有了两种计算方法。

//两点间距离比较近
function getDistance($lat1, $lng1, $lat2, $lng2) 
{ 
 $earthRadius = 6367000; //地球半径m

 $lat1 = ($lat1 * pi() ) / 180; 
 $lng1 = ($lng1 * pi() ) / 180; 

 $lat2 = ($lat2 * pi() ) / 180; 
 $lng2 = ($lng2 * pi() ) / 180; 

 $calcLongitude = $lng2 - $lng1; 
 $calcLatitude = $lat2 - $lat1; 
 $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); 
 $stepTwo = 2 * asin(min(1, sqrt($stepOne))); 
 $calculatedDistance = $earthRadius * $stepTwo; 

 return round($calculatedDistance); 
} 

//两点间距离比较远
function getLongDistance($lat1, $lng1, $lat2,$lng2,$radius = 6378.137)
{
 $rad = floatval(M_PI / 180.0);

 $lat1 = floatval($lat1) * $rad;
 $lng1 = floatval($lng1) * $rad;
 $lat2 = floatval($lat2) * $rad;
 $lng2 = floatval($lng2) * $rad;

 $theta = $lng2 - $lng1;

 $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));

 if ($dist < 0 ) {
  $dist += M_PI;
 }
 return $dist = $dist * $radius;
}

小编再为大家分享一段php坐标之间距离的求解代码:

 <?php 
 define('EARTH_RADIUS', 6378.137);//地球半径 
 define('PI', 3.1415926); 
 /** 
 * 计算两组经纬度坐标 之间的距离 
 * params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km); 
 * return m or km 
 */ 
 function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) 
 { 
 $radLat1 = $lat1 * PI / 180.0; 
 $radLat2 = $lat2 * PI / 180.0; 
 $a = $radLat1 - $radLat2; 
 $b = ($lng1 * PI / 180.0) - ($lng2 * PI / 180.0); 
 $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); 
 $s = $s * EARTH_RADIUS; 
 $s = round($s * 1000); 
 if ($len_type > 1) 
 { 
 $s /= 1000; 
 } 
 return round($s, $decimal); 
 } 
 echo GetDistance(39.908156,116.4767, 39.908452,116.450479, 1);//输出距离/米

上述就是小编为大家分享的使用php怎么对两坐标点之间的距离进行计算了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI