$cha1 = new Model(); $shops = $cha1->query("select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*(".$lat."-lat)/360),2)+COS(3.1415926535898*".$lat."/180)* COS(lat * 3.1415926535898/180)*POW(SIN(3.1415926535898*(".$lng."-lng)/360),2))))*1000 as juli from `wifi_shop` where shoplevel like '%".$type."%' and province = '".$_SESSION['shop']['province']."' and city = '".$_SESSION['shop']['city']."' order by juli asc limit ".$Page->firstRow.",".$Page->listRows);
代碼中$lat和$lng就是已知的那個坐標的經緯度,排序出來的單位是米因為我乘以1000了,where后面的查詢條件可以根據需要編輯,limit后面是分頁的,可以不要或者自己編輯
然后php代碼中計算出兩個坐標點距離的方法是,這個方法也是返回的單位米
function GetDistance($lat1, $lng1, $lat2, $lng2){ define('PI',3.1415926535898); define('EARTH_RADIUS',6378.137); $radLat1 = $lat1 * (PI / 180); $radLat2 = $lat2 * (PI / 180); $a = $radLat1 - $radLat2; $b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180)); $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 * 10000) / 10000; return $s*1000; }