当前位置: 首页 > news >正文

关于根据距离列表排序sql

//第一种  根据经纬度排序       $map = "1=1 ";if ($loadCode && $unloadCode) {$map .= " AND load_city_id = $loadCode and unload_city_id = $unloadCode";} elseif($loadCode) {$map .= " AND load_city_id = $loadCode";} elseif($unloadCode) {$map .= " AND unload_city_id = $unloadCode";}if ($status > 1) {$map .= " AND driver_id = $driverId AND status = $status";} else {$map .= " AND pay_type IS NOT NULL";if($ismy) {$map .= " AND driver_id = $driverId";} else {$map .= " AND status = 1";  }}$virtual_open = config('site.virtual_open');if($virtual_open) {$map .= " AND is_show = 1";} else {$map .= " AND is_real = 1";}$sql = "SELECT *,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(({$lat} * PI() / 180 - load_lat * PI() / 180) / 2), 2) + COS({$lat} * PI() / 180) * COS(load_lat * PI() / 180) * POW(SIN(({$lng} * PI() / 180 - load_lng * PI() / 180) / 2), 2))) * 1000) AS distance FROM fa_order WHERE $map ORDER BY distance ASC LIMIT ".$start.",".$pagesize;$list = Db::query($sql);        


//第二种  附近商家public function getStoreByDiscount(){$lat = $this->request->param("lat");$lng = $this->request->param("lng");$coord = get_coord_by_lat_lng($lat,$lng,5);$list = $this->model->field("id,name,logo_image,telephone,location_x,location_y")->where('location_x', '>=', $coord['right-bottom']['lat'])->where('location_x', '<=', $coord['left-top']['lat'])->where('location_y', '>=', $coord['left-top']['lng'])->where('location_y', '<=', $coord['right-bottom']['lng'])->select();$store = [];foreach($list as $key => $val) {$list[$key]["distance"] = getDistance($lat,$lng,$val["location_x"],$val["location_y"]);}$distance = array_column($list,'distance');array_multisort($distance,SORT_ASC,$list);$this->success("success", $list);}

/*** 计算两组经纬度坐标 之间的距离* params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);* return m or km*/
function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
{$pi = 3.1415926000000001;$er = 6371;$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 * $er;$s = round($s * 1000);if (1 < $len_type) {$s /= 1000;}return round($s, $decimal);
}/*根据当前定位获取附近距离的范围*/
function get_coord_by_lat_lng($lat, $lng, $distance = 2)
{// 定义地球的半径define('EARTH_RADIUS', 6371);$new_lng =  2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));$new_lng = rad2deg($new_lng);$new_lat = $distance / EARTH_RADIUS;$new_lat = rad2deg($new_lat);return ['left-top' => ['lat' => $lat + $new_lat,'lng' => $lng-$new_lng],'right-top' => ['lat' => $lat + $new_lat, 'lng' => $lng + $new_lng],'left-bottom' => ['lat' => $lat - $new_lat, 'lng' => $lng - $new_lng],'right-bottom' => ['lat' => $lat - $new_lat, 'lng' => $lng + $new_lng]];
}
 

 

 

 

 

http://www.hskmm.com/?act=detail&tid=28066

相关文章:

  • 2025年高适配铝型材厂家推荐:深圳市方达铝业领衔,3家企业覆盖多场景
  • 注册c模块到lua中使用
  • DNA权威书籍
  • 220V转5V500mA非隔离电源芯片WT5105
  • linux基础-find查找
  • 220V转12V电机水泵供电驱动WT5105
  • 禁止或启用键盘修改输入法
  • ansys安装时无法修改安装路径--灰色无法修改
  • Qwen2.5-VL技术报告
  • 攸米知识付费小程序管理系统:一站式知识变现解决方案
  • citus设置密码
  • 实践与认识及其发展规律
  • 2025 年最新推荐氨糖厂家排行榜:四期临床验证 + 蓝帽认证,帮你选出优质氨糖软骨素 / 氨糖钙 / 氨糖钙片厂家
  • 完整教程:阿里云瓴羊发布企业级AI智能体服务平台AgentOne,成就AI时代的“超级公司”
  • 利用MCP Server革新软件测试:更智能、更高效的自动化
  • 数据结构-设计一个算法删除单链表L(有头节点)中的最小结点。
  • 可视化图解算法63:单词搜索
  • Playwright MCP在UI回归测试中的实战:构建AI自主测试智能体
  • 基于多平台协同的芯片制造数据治理体系架构与实践
  • 华为设备实现单臂路由
  • vscode中配置c_c++环境
  • Playwright MCP 浏览器自动化框架全面解析
  • Spring中使用Apache Http客户端调第三方系统接口临时查看请求体参数 - 指南
  • vscode 设置c/c++代码格式化插件
  • Microsoft.AspNetCore.Builder
  • 【往届已检索|EI稳定检索】第五届信息技术与当代体育国际学术会议(TCS 2025)
  • windows版本 中间件更新问题记录
  • AI大模型应用实践 八:如何通过RAG数据库实现大模型的私有化定制与优化
  • 【完结11章】基于Golang+Gin+Gorm+Vue3母婴商城项目实战
  • 25-1010 从房间回声看懂离散卷积原理