查询距离最近的查询

    //计算距离
    public function typeStoreList($type_id,$lat,$lng,$distance=10){
  
        $page=$this->request->param('page',1);
        $limit=$this->request->param('limit',10);

        $Map=new Map();
        $squares=$Map->getSquare($lat,$lng,$distance);//获取矩阵
        $where=[];
        $where[]=['status','=',1];
        $data['rows']=$this->field("*,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$lat}*PI()/180-lat*PI()/180)/2),2)+COS({$lat}*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lng*PI()/180-lng*PI()/180)/2),2)))*1000) AS distan")
            ->whereRaw("FIND_IN_SET($type_id,store_type_id)")
            ->where($where)
            ->where('lat','between',[$squares['left-bottom']['lat'],$squares['left-top']['lat']])
            ->where('lng','between',[$squares['left-bottom']['lng'],$squares['right-bottom']['lng']])
            ->order('distan','asc')
            ->page($page,$limit)
            ->select();

        foreach($data['rows'] as $k=>$v){
            $distance=$Map->getDistanceBetweenPointsNew($v['lat'], $v['lng'], $lat, $lng);
            $data['rows'][$k]['distance']=$distance;
        }

        $data['total']=$data['rows']->count();
        $res=['status'=>true,'msg'=>'success','data'=>$data];
        return $res;
    }

猜你喜欢

转载自blog.csdn.net/u013373006/article/details/120005292