哪些是企业网站,英国做电商网站有哪些方面,没技术怎么做网站,宠物用品网站开发背景需求#xff0c;数据表中有lng#xff08;经度#xff09;lat#xff08;维度#xff09;两个字段#xff0c;查询数据的时候要计算记录经纬度距离目标经纬度之间的距离。
方法中还有根据生日计算年龄(YEAR(CURDATE()) - YEAR(birthday)) AS age public function get_li…需求数据表中有lng经度lat维度两个字段查询数据的时候要计算记录经纬度距离目标经纬度之间的距离。
方法中还有根据生日计算年龄(YEAR(CURDATE()) - YEAR(birthday)) AS age public function get_lists(){$request $this-request-get();
//获取前端传来的目标经纬度if(empty($request[lng]) || empty($request[lat])){$this-error(位置信息错误);}$where [];$lists PurposeModel::alias(p)-join(user u,p.user_id u.id)-join(resume r,p.user_id r.user_id)-join(category c,p.category_id c.id)-where($where)//根据生日计算年龄(YEAR(CURDATE()) - YEAR(birthday)) AS age 根据经纬度计算距离-field(p.user_id,r.name,u.avatar,(YEAR(CURDATE()) - YEAR(birthday)) AS age,r.sex,c.name as category_name,p.gongzhong,p.work_begin,p.work_end,p.rest_begint,p.rest_end,p.city,p.address,.getDistanceBuilder($request[lat], $request[lng]))-paginate();$this-success(获取成功,$lists);}
在common.php中添加下面的方法
if (!function_exists(getDistanceBuilder)) {/*** 数据库查询的时候计算两点距离命名为 distance 字段* param string $lat 维度* param string $lng 经度* 返回的距离字段 distance 单位是米* p.lat是我的表别名和维度字段名* p.lng是我的表别名和经度字段名* 字段名要根据自己的表的别名进行修改* return string*/function getDistanceBuilder($lat, $lng) {return ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((. matchLatLng($lat) . * PI() / 180 - p.lat * PI() / 180) / 2), 2) COS(. matchLatLng($lat). * PI() / 180) * COS(p.lat * PI() / 180) * POW(SIN((. matchLatLng($lng). * PI() / 180 - p.lng * PI() / 180) / 2), 2))) * 1000) AS distance;}
}if (!function_exists(matchLatLng)) {function matchLatLng($latlng) {$match /^\d{1,3}\.\d{1,30}$/;return preg_match($match, $latlng) ? $latlng : 0;}
}
代码就可以实现在查询结果中增加distance列用于记录两个经纬度坐标之间的距离可该字段进行排序。