zoukankan      html  css  js  c++  java
  • 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; 
        }
  • 相关阅读:
    第二次作业
    大学——新生活方式
    第四次作业
    第三次作业
    第二次作业——起航
    梦开始的地方
    第四次作业
    第三次作业
    第二次作业
    博客作业 随笔
  • 原文地址:https://www.cnblogs.com/zonglonglong/p/4103566.html
Copyright © 2011-2022 走看看