CREATE FUNCTION `lat_lng_distance` (lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT)
RETURNS FLOAT
DETERMINISTIC
BEGIN
    RETURN ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((lat1 * PI() / 180 - lat2 * PI() / 180) / 2), 2)
           + COS(lat1 * PI() / 180) * COS(lat2 * PI() / 180)
           * POW(SIN(( lon1 * PI() / 180 - lon2 * PI() / 180 ) / 2),2))),2);
END

创建完成,然后再在sql语句中调用

$shops = Db::name('XXXX')
  ->field('id,lat_lng_distance('.$lat.','.$lon.',lat,lon) as distance')
  ->where($where)
  ->page($page,10)
  ->order('distance asc')
  ->select()->toArray();

 

 posted on 2020-08-25 17:29  三哥-Noble  阅读(337)  评论(0编辑  收藏  举报
TOP