微信小程序-地图上的图标计算旋转值朝向经纬度计算

废话不多说,开整

js 复制代码
// 参数为寄件人经纬度和收件人经纬度 
// 根据寄收件人经纬度弧度π进行rotate旋转计算
const getRotate = (po1, po2) => {
    if (!(po1 && po2)) return 0
    const lng_a = po1.longitude
    const lat_a = po1.latitude
    const lng_b = po2.longitude
    const lat_b = po2.latitude
    const a = ((90 - lat_b) * Math.PI) / 180
    const b = ((90 - lat_a) * Math.PI) / 180
    const AOC_BOC = ((lng_b - lng_a) * Math.PI) / 180
    const cosc = Math.cos(a) * Math.cos(b) + Math.sin(a) * Math.sin(b) * Math.cos(AOC_BOC)
    const sinc = Math.sqrt(1 - cosc * cosc)
    const sinA = (Math.sin(a) * Math.sin(AOC_BOC)) / sinc
    const A = (Math.asin(sinA) * 180) / Math.PI
    let rotate = 0
    if (lng_b > lng_a && lat_b > lat_a) rotate = A
    else if (lng_b > lng_a && lat_b < lat_a) rotate = 180 - A
    else if (lng_b < lng_a && lat_b < lat_a) rotate = 180 - A
    else if (lng_b < lng_a && lat_b > lat_a) rotate = 360 + A
    else if (lng_b > lng_a && lat_b == lat_a) rotate = 90
    else if (lng_b < lng_a && lat_b == lat_a) rotate = 270
    else if (lng_b == lng_a && lat_b > lat_a) rotate = 0
    else if (lng_b == lng_a && lat_b < lat_a) rotate = 180

    if (rotate > 360) {
      rotate = rotate - 360
    }
    return rotate
  }
相关推荐
icebreaker13 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker14 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
大米饭消灭者3 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
FliPPeDround4 天前
Vitest Environment UniApp:让 uni-app E2E 测试变得前所未有的简单
微信小程序·e2e·前端工程化
FliPPeDround4 天前
微信小程序自动化的 AI 新时代:wechat-devtools-mcp 智能方案
微信小程序·ai编程·mcp
吴声子夜歌5 天前
小程序——布局示例
小程序
码云数智-大飞5 天前
如何创建自己的小程序,码云数智与有赞平台对比
微信小程序
luffy54595 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序
Slow菜鸟5 天前
微信小程序开发(二)目录结构完全指南
微信小程序·小程序
攀登的牵牛花5 天前
给女朋友写了个轻断食小程序:去老丈人家也是先动筷了
前端·微信小程序