title: Calculate vertical bearing between two GPS coordinates with altitudes
body: <p>I am planning to build an antenna tracker. I need to get bearing and tilt from GPS point A with altitude and GPS point B with altitude.</p>
  
  <p>This is the example points:</p>
  
  <pre><code>latA = 39.099912
  lonA = -94.581213
  altA = 273.543
  latB = 38.627089
  lonB = -90.200203
  altB = 1380.245
  </code></pre>
  
  <p>I've already got the formula for horizontal bearing and it gives me 97.89138167122422</p>
  
  <p>This is the code:</p>
  
  <pre><code>function toRadian(num) {
      return num * (Math.PI / 180);
  }
  
  function toDegree(num) {
      return num * (180 / Math.PI);
  }
  
  function getHorizontalBearing(fromLat, fromLon, toLat, toLon) {
      fromLat = toRadian(fromLat);
      fromLon = toRadian(fromLon);
      toLat = toRadian(toLat);
      toLon = toRadian(toLon);
  
      let dLon = toLon - fromLon;
      let x = Math.tan(toLat / 2 + Math.PI / 4);
      let y = Math.tan(fromLat / 2 + Math.PI / 4);
      let dPhi = Math.log(x / y);
      if (Math.abs(dLon) &gt; Math.PI) {
          if (dLon &gt; 0.0) {
              dLon = -(2 * Math.PI - dLon);
          } else {
              dLon = (2 * Math.PI + dLon);
          }
      }
  
      return (toDegree(Math.atan2(dLon, dPhi)) + 360) % 360;
  }
  
  let n = getHorizontalBearing(39.099912, -94.581213, 38.627089, -90.200203);
  console.info(n);
  </code></pre>
  
  <p>But I don't know how to find the tilt angle. Anyone could help me?</p>