数据库根据经纬度查询数据并根据距离排序:
SELECT *, ROUND( 6378.138*2*ASIN( SQRT( POW( SIN( (纬度*PI()/180-latitude*PI()/180)/2 ),2 )+COS(纬度*PI()/180)*COS(latitude*PI()/180)*POW( SIN( (经度*PI()/180-longitude*PI()/180)/2 ),2 ) ) )*1000 ) AS lang FROM `circle_community` ORDER BY lang ASC"
PHP根据两点的经纬度计算距离
function radian($d) { return $d * 3.1415926535898 / 180.0; } function distanceCalculate($longitude1, $latitude1, $longitude2, $latitude2) { $radLat1 = radian ( $latitude1 ); $radLat2 = radian ( $latitude2 ); $a = radian ( $latitude1 ) - radian ( $latitude2 ); $b = radian ( $longitude1 ) - radian ( $longitude2 ); $s = 2 * asin ( sqrt ( pow ( sin ( $a / 2 ), 2 ) + cos ( $radLat1 ) * cos ( $radLat2 ) * pow ( sin ( $b / 2 ), 2 ) ) ); $s = $s * 6378.137; //乘上地球半径,单位为公里 $s = round ( $s * 10000 ) / 10000; //单位为公里(km) return $s; //单位为km }