title: Get direction (compass) with two longitude/latitude points
body: <p>I'm working on a &quot;compass&quot; for a mobile-device. I have the following points:</p>
  <pre><code>point 1 (current location): Latitude = 47.2246, Longitude = 8.8257
  point 2 (target  location): Latitude = 50.9246, Longitude = 10.2257
  </code></pre>
  <p>Also I have the following information (from my android-phone):</p>
  <pre><code>The compass-direction in degree, which bears to the north. 
  For example, when I direct my phone to north, I get 0°
  </code></pre>
  <p>How can I create a &quot;compass-like&quot; arrow which shows me the direction to the point?</p>
  <p>Is there a mathematic-problem for this?</p>
  <p>EDIT: Okay I found a solution, it looks like this:</p>
  <pre><code>/**
   * Params: lat1, long1 =&gt; Latitude and Longitude of current point
   *         lat2, long2 =&gt; Latitude and Longitude of target  point
   *         
   *         headX       =&gt; x-Value of built-in phone-compass
   * 
   * Returns the degree of a direction from current point to target point
   *
   */
  function getDegrees(lat1, long1, lat2, long2, headX) {
      
      var dLat = toRad(lat2-lat1);
      var dLon = toRad(lon2-lon1);
  
      lat1 = toRad(lat1);
      lat2 = toRad(lat2);
  
      var y = Math.sin(dLon) * Math.cos(lat2);
      var x = Math.cos(lat1)*Math.sin(lat2) -
              Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
      var brng = toDeg(Math.atan2(y, x));
  
      // fix negative degrees
      if(brng&lt;0) {
          brng=360-Math.abs(brng);
      }
  
      return brng - headX;
  }
  </code></pre>