当前位置:首页 > PHP教程 > PHP高级教程

PHP根据两点间的经纬度计算距离

这是一个不错的示例,直接贴代码,首先要知道纬度值、经度值

/** 
* @desc 根据两点间的经纬度计算距离 
* @param float $lat 纬度值 
* @param float $lng 经度值 
*/ 
function getdistance($lat1, $lng1, $lat2, $lng2) 
{ 
$earthradius = 6367000; //approximate radius of earth in meters 

/* 
convert these degrees to radians 
to work with the formula 
*/ 

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

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

/* 
using the 
haversine formula 

http://en.wikipedia.org/wiki/haversine_formula 

calculate the distance 
*/ 

$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); 
} 

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!