时间:2021-07-01 10:21:17 帮助过:21人阅读
mysql函数-根据经纬度坐标计算距离 在实际的Web开发中,越来越多的碰到一些特定领域的专业算法,比如本文提到的根据两点经纬度计算距离的方法。 通常的两种计算: 1。要算的距离是椭球面的距离 设第一点A的经纬度为(LonA, LatA),第二点B的经纬度为(LonB, LatB
mysql函数-根据经纬度坐标计算距离
class Distance{
const EARTH_RADIUS = 6378.137; // earth radius (const) kilometer
/**
*
* Example:
* $precision = 49;
* ini_set("precision", $precision);
* echo pi(); //will output 3.141592653589793115997963468544185161590576171875
*
* 1 kilometer = 0.621371192 mile
*
* @param double d
* @return double data
*/
private function rad( $d )
{
return $d * M_PI / 180.0;
}
/**
*
* Example : get_distance(44.2112,-88.4175,34.5082,-82.6498)
* Get Distance between two point : longitude,latitude(lat1,lng1 => lat2,lng2 )
*
* @param double d
* @return double data
*
*/
public function get_distance( $lat1, $lng1, $lat2, $lng2, $base = 1000 )
{
$radLat1 = floatval( $this->rad($lat1) );
$radLat2 = floatval( $this->rad($lat2) );
$a = $radLat1 - $radLat2;
$b = $this->rad($lng1) - $this->rad($lng2);
$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
$s = $s * self::EARTH_RADIUS;
$s = round($s * 10000) / 10000;
return $s;
}
}