title: Geographic Midpoint between two coordinates
body: <p>I have been using the Moveable-Type website to aid me in some Geocoordinate calcuations and it's been very useful, however, I have a bug in my calculation of the mid-point between two coordinates.  My result is close to the expected, but not close enough:</p>
  
  <pre><code>posA = {47.64570362, -122.14073746}
  posB = {47.64316917, -122.14032175}
  </code></pre>
  
  <p>expected result (taken from the movable type calculator) = 47°38′40″N, 122°08′26″W = <code>{47.644444, -122.140556}</code> my result: <code>{49.6054801645915, -122.14052959995759}</code></p>
  
  <p>Here is my code:</p>
  
  <pre><code>private Geocoordinate MidPoint(Geocoordinate posA, Geocoordinate posB)
  {
     Geocoordinate midPoint = new Geocoordinate();
  
     double dLon = DegreesToRadians(posB.Longitude - posA.Longitude);
     double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon);
     double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon);
  
     midPoint.Latitude = RadiansToDegrees(Math.Atan2(Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)), 
                  Math.Sqrt((Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) * (Math.Cos(DegreesToRadians(posA.Latitude))) + Bx) + By * By));
  
     midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx));
  
     return midPoint;
  }
  </code></pre>
  
  <p>I've got a couple of private methods to do the conversion between Degrees and Radians and back. 
  E.g.</p>
  
  <pre><code>private double DegreeToRadian(double angle)
  {
     return Math.PI * angle / 180.0;
  }
  </code></pre>
  
  <p>I can't work out why my results are off by a couple of degrees on the Lat value.  Any ideas?</p>
  
  <p>Thanks</p>