当前位置: 首页> 汽车> 维修 > 5118网站的功能_国际市场调研公司_中国网站排名查询_seo推广费用需要多少

5118网站的功能_国际市场调研公司_中国网站排名查询_seo推广费用需要多少

时间:2025/7/30 4:37:59来源:https://blog.csdn.net/qq_32058147/article/details/143471592 浏览次数: 0次
5118网站的功能_国际市场调研公司_中国网站排名查询_seo推广费用需要多少

1.核心思路

1.利用sequelize的fn方法调用MySql原生函数(st_distance_sphere、point)

2.这里利用到了MySql的原生函数,不懂可以去看看mysql的函数知识

2.核心代码

//st_distance_sphere、point函数用来计算当前经纬度和目的地经纬度
//col为获取表头的字段名
//最后将计算完的值命名为"distance"
fn('st_distance_sphere' ,  fn('point', col('lng'), col('lat')) ,  fn('point', lng , lat)  )   , "distance" //示例
fn('st_distance_sphere' ,  fn('point', col('需要查询经度'), col('需要查询纬度')) ,  fn('point', 你的经度, 你的纬度)  )   , "distance" 

3.完整代码

const FindAll = async (ctx) => {try {const { pageNum, pageSize , lng , lat } = ctx.query;if (!pageNum && !pageSize) {return ctx.app.emit('error', parameterMissingError, ctx)}const res = await Works.findAndCountAll({raw: true,nest: true,attributes:{include:[// 原生函数计算距离[ fn('st_distance_sphere' ,  fn('point', col('lng'), col('lat')) ,  fn('point', lng , lat)  )   , "distance" ]]  },include: [{as: 'user',model: User,attributes: ['nickName']}],limit: pageSize * 1,offset: (pageNum - 1) * pageSize * 1,order: [//根据距离排序[ fn('st_distance_sphere' ,  fn('point', col('lng'), col('lat')) ,  fn('point', lng , lat)  ) , 'DESC' ]],})ctx.body = { code: 200, msg: '查询成功', data: res }} catch (err) {console.log(err)return ctx.app.emit('error', findError, ctx)}
}

4.效果展示

distance就是查询到的参数,需要用parseFloat((distance/1000).toFixed(2)) ; 转成km和保存两位小数

关键字:5118网站的功能_国际市场调研公司_中国网站排名查询_seo推广费用需要多少

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: