当前位置: 首页 > 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.jsqmd.com/news/11007/

相关文章:

  • 2025年高适配铝型材厂家推荐:深圳市方达铝业领衔,3家企业覆盖多场景
  • 注册c模块到lua中使用
  • S3Bucket安全评分与合规状态逻辑修复:全面提升云存储安全评估准确性 - 详解
  • DNA权威书籍
  • 220V转5V500mA非隔离电源芯片WT5105
  • linux基础-find查找
  • 220V转12V电机水泵供电驱动WT5105
  • 禁止或启用键盘修改输入法
  • 智能提取表格从pdf, 图片 到 excel, csv
  • 实践与认识及其发展规律
  • 完整教程:阿里云瓴羊发布企业级AI智能体服务平台AgentOne,成就AI时代的“超级公司”
  • 利用MCP Server革新软件测试:更智能、更高效的自动化
  • 数据结构-设计一个算法删除单链表L(有头节点)中的最小结点。
  • 可视化图解算法63:单词搜索
  • AI大模型应用实践 八:如何通过RAG数据库实现大模型的私有化定制与优化
  • ARM芯片架构之CoreSight高效的系统架构规范
  • 【完结11章】基于Golang+Gin+Gorm+Vue3母婴商城项目实战
  • 25-1010 从房间回声看懂离散卷积原理
  • (13)ASP.NET Core2.2 中的选项模式(Options) - 教程
  • 如何设计10亿用户级的微博Feed流系统并应对100W QPS的挑战?
  • 印度尼西亚股票实时数据API对接文档
  • 2025 年铝门窗厂家推荐榜,系统 / 智能 / 断桥 / 窄边 / 定制 / 全景 / 阳光房 / 隐框 / 隔声 / 防火铝门窗公司推荐
  • 如何播放 M3U8 格式的视频
  • 20232304 2025-2026-1 《网络与系统攻防技术》实验一实验报告
  • lsh 的源码注释(1)
  • Codeforces Round 1056 (Div. 2) A~D
  • 现代软件工程阅读和提问作业-1
  • 一种CDN动态加速回源白名单选路及降低源站探测量的方法
  • Windows系统-应用问题全面剖析Ⅰ:德承工控机DA-1200在Windows操作系统下[开机黑屏]的解决方法 - Johnny
  • Java文件路径/服务器路径的获取