Laravel 框架根据经纬度计算在一定距离内的数据

   
$model = DB::table('table_name');
public static function scope_distance($model, $from_latitude, $from_longitude, $distance)
{
        $raw = DB::raw("round(st_distance_sphere(point($from_latitude, $from_longitude),point(latitude , longitude)), 2)/1000  AS distance");
        return $model->select('*')->addSelect($raw)->orderBy('distance', 'ASC')->having('distance', '<=', $distance);
}

注:
st_distance_sphere 函数详见MySQL5.7版本说明
参:https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html

 

posted @ 2023-02-22 15:35  xiager  阅读(83)  评论(0编辑  收藏  举报