PHP经过经纬度计算距离

SQL语句查询并计算距离php

$lat = 40.003661; // 当前纬度 纬度最高90
$lng = 116.326510; // 当前经度
// latitude 纬度
// longitude 经度
$page= $page> 0 ? $page - 1 : $page;
$distance = "(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*(" . $lat . "-latitude)/360),2)+COS(3.1415926535898*" . $lat . "/180)* COS(latitude * 3.1415926535898/180)*POW(SIN(3.1415926535898*(" . $lng . "-longitude)/360),2))))*1000";
$sql = "select *," . $distance . " as juli from About where " . $where . " order by juli ASC, addtime DESC limit " . $page. "," . $limit;
$arr = Db::query($sql);
$count = count($arr);

php自定义函数计算距离git

/*
 * 地图计算距离
 *  $lat1:起点纬度
 *  $lng1:起点经度
 *
 *  $lat2:终点纬度
 *  $lng2:终点经度
 * */
function TX_Map_Api_distance($lat1, $lng1, $lat2, $lng2)
{
    // 将角度转为狐度
    $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 bcadd($s, 0, 2);
}