Find Destination (Lat,Lng) Given Distance & Bearing

Here, I will be posting a C# implementation to find a destination (latitude, longitude) given distance and bearing. This will be continuation from my previous post. Just add the method to the skeleton class GLatLng.

public GLatLng DestinationPoint(double dist, double brng, DistanceType dType)
{
    double R = (dType == DistanceType.Miles) ? EarthRadiusInMiles : EarthRadiusInKilometers;
    dist = dist / R;
    brng = Math.PI * brng / 180;
    double lat1 = DegreeToRadian(latitude);
    double lon1 = DegreeToRadian(longitude);
<p>    double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(dist) + Math.Cos(lat1) * Math.Sin(dist) * Math.Cos(brng));
    double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(dist) * Math.Cos(lat1), Math.Cos(dist) - Math.Sin(lat1) * Math.Sin(lat2));
<p>    lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
    lat2 = RadianToDegree(lat2);
    lon2 = RadianToDegree(lon2);
<p>    GLatLng newLatLng = new GLatLng(lat2, lon2);
    return newLatLng;
} // end DestinationPoint
public GLatLng RhumbDestinationPoint(double dist, double brng, DistanceType dType)
{
    double R = (dType == DistanceType.Miles) ? EarthRadiusInMiles : EarthRadiusInKilometers;
    double d = dist / R;  
    double lat1 = DegreeToRadian(this.latitude);
    double lon1 = DegreeToRadian(this.longitude);
    brng = DegreeToRadian(brng);
    double lat2 = lat1 + d * Math.Cos(brng);
    double dLat = lat2 - lat1;
    double dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
    double q = Math.Cos(lat1);
    if (dPhi != 0) q = dLat / dPhi;  // E-W line gives dPhi=0
    double dLon = d * Math.Sin(brng) / q;
     if (Math.Abs(lat2) &gt; Math.PI / 2) lat2 = (lat2 &gt; 0) ? Math.PI - lat2 : -(Math.PI - lat2);
    double lon2 = (lon1 + dLon + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
   return new GLatLng(RadianToDegree(lat2), RadianToDegree(lon2));
} // end RhumbDestinationPoint
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s