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 @   xiager  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示