地图相关算法(角度,方向类)

1、根据三个点计算角度

核心代码如下:

let _P1P2 = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2);
let _P2P3 = Math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2 + (z3 - z2) ** 2);
let P = (x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2) + (z1 - z2) * (z3 - z2);
var angle = (Math.acos(P / (_P1P2 * _P2P3)) / Math.PI) * 180;

在项目中用法:

pos.forEach(function(val,index){
    if (index > 0 && index < pos.length-1) {
    
        var x1= pos[index-1].x
        var x2= pos[index].x
        var x3= pos[index+1].x
        var y1= pos[index-1].y
        var y2= pos[index].y
        var y3= pos[index+1].y
        var z1= pos[index-1].z
        var z2= pos[index].z
        var z3= pos[index+1].z
        
        let _P1P2 = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2);
        let _P2P3 = Math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2 + (z3 - z2) ** 2);
        let P = (x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2) + (z1 - z2) * (z3 - z2);
        var angle = (Math.acos(P / (_P1P2 * _P2P3)) / Math.PI) * 180;
        
        if (angle && !(isNaN(angle))) {
            Viewer.entities.add({
                position :new Cesium.Cartesian3(val.x, val.y, val.z+100),
                label : {
                text: angle.toFixed(2) + "°",
                font : '24px sans-serif',
                pixelOffset : new Cesium.Cartesian2(0.0, 20)
        }
        });
    }
    }
})

2、经纬度求与正北方向的夹角(方向角)

核心代码如下:


function getAngel(A, B) {
    var dx = (B.m_RadLo - A.m_RadLo) * A.Ed;
    var dy = (B.m_RadLa - A.m_RadLa) * A.Ec;
    var angle = 0.0;
    angle = Math.atan(Math.abs(dx / dy)) * 180. / Math.PI;
    var dLo = B.m_Longitude - A.m_Longitude;
    var dLa = B.m_Latitude - A.m_Latitude;
    if (dLo > 0 && dLa <= 0) {
        angle = (90. - angle) + 90;
    } else if (dLo <= 0 && dLa < 0) {
        angle = angle + 180;
    } else if (dLo < 0 && dLa >= 0) {
        angle = (90 - angle) + 270;
    }
    return angle;
}

function MyLatLng(longitude, latitude) {
    var Rc = 6378137;
    var Rj = 6356725;
    var m_LoDeg, m_LoMin, m_LoSec;
    var m_LaDeg, m_LaMin, m_LaSec;
    var m_Longitude, m_Latitude;
    var m_RadLo, m_RadLa;
    var Ec;
    var Ed;
    
    m_LoDeg = longitude;
    m_LoMin = Number.parseInt((longitude - m_LoDeg) * 60);
    m_LoSec = (longitude - m_LoDeg - m_LoMin / 60.) * 3600;
    m_LaDeg = latitude;
    m_LaMin = Number.parseInt((latitude - m_LaDeg) * 60);
    m_LaSec = (latitude - m_LaDeg - m_LaMin / 60.) * 3600;
    m_Longitude = longitude;
    m_Latitude = latitude;
    m_RadLo = longitude * Math.PI / 180.;
    m_RadLa = latitude * Math.PI / 180.;
    Ec = Rj + (Rc - Rj) * (90. - m_Latitude) / 90.;
    Ed = Ec * Math.cos(m_RadLa);
    
    return {
        m_RadLo: m_RadLo,
        m_RadLa: m_RadLa,
        m_Longitude: m_Longitude,
        m_Latitude: m_Latitude,
        Ed: Ed,
        Ec: Ec
    }
}


var ag = getAngel(MyLatLng(120, 40), MyLatLng(110, 25))
ag = Number.parseInt(ag)
if (ag == 0) {
    ag = "正北"
} else if (ag > 0 && ag < 90) {
    ag = "北偏东" + ag
} else if (ag == 90) {
    ag = "正东"
} else if (ag > 90 && ag < 180) {
    ag = "南偏东" + (180 - ag)
} else if (ag == 180) {
    ag = "正南"
} else if (ag > 180 && ag < 270) {
    ag = "南偏西" + (ag - 180)
} else if (ag == 270) {
    ag = "正西"
} else if (ag > 270 && ag < 360) {
    ag = "北偏西" + (360 - ag)
} else if (ag == 360) {
    ag = "正北"
}

image.png

项目中用法:

pos.forEach(function(val,index){
             if (index < pos.length-1) {
              var x11= pos[index].x
              var x21= pos[index+1].x
              var y11= pos[index].y
              var y21= pos[index+1].y
              var z11= pos[index].z
              var z21= pos[index+1].z

              var ellipsoid=Viewer.scene.globe.ellipsoid;

              var cartesian3=new Cesium.Cartesian3(x11,y11,z11);
              var cartograhphic=ellipsoid.cartesianToCartographic(cartesian3);
              var lat=Cesium.Math.toDegrees(cartograhphic.latitude);
              var lng=Cesium.Math.toDegrees(cartograhphic.longitude);

              var cartesian32=new Cesium.Cartesian3(x21,y21,z21);
              var cartograhphic2=ellipsoid.cartesianToCartographic(cartesian32);
              var lat2=Cesium.Math.toDegrees(cartograhphic2.latitude);
              var lng2=Cesium.Math.toDegrees(cartograhphic2.longitude);

              var cartographic=Cesium.Cartographic.fromDegrees((lng2+lng)/2,(lat2+lat)/2,200);
              var cartesian33 = ellipsoid.cartographicToCartesian(cartographic);

              var ag = that.getAngel (that.MyLatLng(lng, lat),that.MyLatLng(lng2, lat2));
              ag = Number.parseInt(ag)
              if (ag == 0) {
                ag = "正北"
              }else if (ag > 0 && ag < 90) {
                ag = "北偏东" + ag
              }else if (ag == 90) {
                ag = "正东"
              }else if (ag > 90 && ag < 180) {
                ag = "南偏东" + (180-ag)
              }else if (ag == 180) {
                ag = "正南"
              }else if (ag > 180 && ag < 270) {
                ag = "南偏西" + (ag-180)
              }else if (ag == 270) {
                 ag = "正西"
              }else if (ag > 270 && ag < 360) {
                ag = "北偏西" + (360-ag)
              }else if (ag == 360) {
                 ag = "正北"
              }
              Viewer.entities.add({
                  position :cartesian33,
                  label : {
                      text: ag + "°",
                      font : '24px sans-serif',
                      pixelOffset : new Cesium.Cartesian2(0.0, 20)
                  }
              });
            }
          })

        });

你可能感兴趣的:(地图相关算法(角度,方向类))