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版根据当前定位经纬度排序在我的博客中也有写,文章地址:
<a href="https://www.tiance.club/post/2178671103.html" target='_blank'>https://www.tiance.club/post/2178671103.html</a>
原文作者: xingguang
原文链接: https://www.tiance.club/post/1713014064.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容