根據經緯度計算與正北方向的夾角,以順時針為方向的角度

參考:https://software.intel.com/en-us/blogs/2012/11/30/calculating-a-bearing-between-points-in-location-aware-apps

以下的公式,可以在下列網址驗證

https://www.igismap.com/map-tool/bearing-angle

https://www.sunearthtools.com/tools/distance.php#contents

// 根據經緯度計算與正北方向的夾角角度
bearingDegrees(
   latitude1: number,
   longitude1: number,
   latitude2: number,
   longitude2: number
) {
   // 角度 --> 弧度
   const degreesToRadians = Math.PI / 180.0;

   const phi1 = latitude1 * degreesToRadians;
   const phi2 = latitude2 * degreesToRadians;
   const lam1 = longitude1 * degreesToRadians;
   const lam2 = longitude2 * degreesToRadians;

   const y = Math.sin(lam2 - lam1) * Math.cos(phi2);
   const x =
      Math.cos(phi1) * Math.sin(phi2) -
      Math.sin(phi1) * Math.cos(phi2) * Math.cos(lam2 - lam1);

   const bearing = (Math.atan2(y, x) * 180) / Math.PI;

   return bearing;

   //return Math.atan2(Math.sin(lam2 - lam1) * Math.cos(phi2), Math.cos(phi1) * Math.sin(phi2) - Math.sin(phi1) * Math.cos(phi2) * Math.cos(lam2 - lam1)) * 180 / Math.PI;
}

 

// 根據經緯度計算與正北方向的夾角角度後,以順時針為方向的角度
bearingInitial(
   latitude1: number,
   longitude1: number,
   latitude2: number,
   longitude2: number
) {
   console.log(
      "bearingInitial -->" +
      (
         (this.bearingDegrees(
            latitude1,
            longitude1,
            latitude2,
            longitude2
            ) +
            360) %
         360
      ).toFixed(7)
   );
   return (
      (this.bearingDegrees(latitude1, longitude1, latitude2, longitude2) +
         360) %
      360
   ).toFixed(7);
}

文章標籤

全站熱搜

bingzhichen 發表在 痞客邦 留言(0) 人氣()