一直到最后

导航

php根据当前定位经纬度排序

php根据当前定位经纬度排序

直接贴代码
原文作者: xingguang
原文链接: https://www.tiance.club/post/1713014064.html

/**
     * 计算两组经纬度坐标 之间的距离
     * params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
     * return m or km
     */

    public static function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
        defined('EARTH_RADIUS') || define('EARTH_RADIUS', 6378.137); // 地球半径
        defined('PI') || define('PI', 3.1415926);
        $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);
    }

两组经纬度对比中间的距离 lat1 纬度1; lng1 经度1为第一组,lat2 纬度2; lng2 经度2为第二组,不管两组顺序怎么调换,结果距离都是一样。
另外还有mysql版根据当前定位经纬度排序在我的博客中也有写,文章地址:
https://www.tiance.club/post/2178671104.html
原文作者: xingguang
原文链接: https://www.tiance.club/post/1713014064.html

posted on 2020-03-15 16:33  一直到最后  阅读(631)  评论(0编辑  收藏  举报