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

废话不多说,开整

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
  }
相关推荐
一渊之隔41 分钟前
微信小程序在用户拒绝授权后无法使用wx.opensetting再次获取定位授权
微信小程序·小程序
racerun5 小时前
微信小程序如何实现再多个页面共享数据
微信小程序·小程序
XM-54585 小时前
2025微信小程序wxapkg解包全攻略
linux·运维·小程序
HERR_QQ1 天前
【unify】unify的微信小程序开发学习 (to be continued)
学习·微信小程序·小程序
racerun2 天前
小程序导航设置更多内容的实现方法
小程序
说私域2 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg6682 天前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
程序员柳2 天前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
Jyywww1212 天前
微信小程序学习笔记
笔记·学习·微信小程序