title: How do I find the lat/long that is x km north of a given lat/long?
body: <p>I have some C# code that generates google maps. This codes looks at all the Points I need to plot on the map and then works out the Bounds of a rectangle to include those points. It then passes this bounds to the Google Maps API to set the zoom level appropriately to show all of the points on the map.</p>
  
  <p>This code is working fine however I have a new requirement.</p>
  
  <p>One of the points may have a precision associated with it. If this is the case then I draw a circle around the point with the radius set to the precision value. Again this works fine however my bounds checking is now not doing what I want it to do. I want to have the bounding box include the complete circle.</p>
  
  <p>This requires an algorithm to take a point x and calculate the point y that would be z metres north of x and also z metres south of x.</p>
  
  <p>Does anyone have this algorithm, preferably in C#. I did find a generic algorithm <a href="http://edwilliams.org/avform.htm#Intro" rel="noreferrer">here</a> but I appear to have not implemented this correctly as the answers I am getting are 1000s of km adrift.</p>
  
  <p>This is the Generic example</p>
  
  <pre><code>Lat/lon given radial and distance
  
  A point {lat,lon} is a distance d out on the tc radial from point 1 if:
  
       lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
       IF (cos(lat)=0)
          lon=lon1      // endpoint a pole
       ELSE
          lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
       ENDIF
  </code></pre>
  
  <p>And this is my C# translation. </p>
  
  <pre><code>  // Extend a Point North/South by the specified distance
      public static Point ExtendPoint(Point _pt, int _distance, int _bearing )
      {
          Decimal lat = 0.0;
          Decimal lng = 0.0;
  
          lat = Math.Asin(Math.Sin(_pt.Lat) * Math.Cos(_distance) + Math.Cos(_pt.Lat) * 
              Math.Sin(_distance) * Math.Cos(_bearing));
  
           if (Math.Cos(lat) == 0)
           {
              lng = _pt.Lng;      // endpoint a pole
           }
           else 
           {
               lng = (
                   (_pt.Lng - Math.Asin(Math.Sin(_bearing) * Math.Sin(_distance) / Math.Cos(lat)) 
                   + Math.PI) % (2 * Math.PI)) - Math.PI;
           }
  
           ret = new Point(lat,lng);
           return ret;
      }
  </code></pre>
  
  <p>I am calling this function with a bearing of 0 to calculate the new northerly position and a value of 180 to calculate the new southerly position.</p>
  
  <p>Can anyone either see what I have done wrong or perhaps provide a known working algorithm?</p>