thinkphp mysql 坐标按距离排序


$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; 
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM