mysql、 php 根据经纬度计算距离

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/oZhengTuoJiaSuo/article/details/82500280

mysql 有个RADIANS 函数 等价于 php里面的deg2rad 函数

php:

if (!function_exists('get_distance')) {
    //from ='39.983171,116.308479'
    function get_distance($from,$to)
    {   
        $from_arr=explode(',', $from);
        $to_arr=explode(',', $to);

        $lat1=$from_arr[0];
        $lng1=$from_arr[1];

        $lat2=$to_arr[0];
        $lng2=$to_arr[1];
        $radLat1 = deg2rad($lat1);// deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $s = 2 * asin(sqrt(pow(sin($a / 2), 2)+cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;

        return $s;
    }
    
}

mysql:

SELECT  (2 * asin(sqrt(pow(sin((RADIANS(35.95995) - RADIANS(latitude))/ 2), 2)+cos(RADIANS(35.95995)) * cos(RADIANS(latitude)) * pow(sin((RADIANS(120.19653) -RADIANS(longitude)) / 2), 2))) * 6378.137) as juli FROM fa_circle WHERE id=9;

猜你喜欢

转载自blog.csdn.net/oZhengTuoJiaSuo/article/details/82500280