Tuesday, 19 June 2012

distance between two latitude longitude points in c#



/// Loc A Longitude
/// Loc A Latitude
/// Loc B Longitude
/// Loc B Latitude
/// 1=Km,2=mtr
///
public double distance(doubleX1,double Y1,doubleX2,double Y2,intFlg)
{
  double R = 6371;
  double dLat = this.toRadian(Y2 - Y1);
  double dLon = this.toRadian(X2 - X1);
  double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +  Math.Cos(this.toRadian(Y1)) * Math.Cos(this.toRadian(Y2)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
  double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
  double d = R * c;
  switch (Flg)
  {
      case 1:                 //km
            return d;
            break;
      case2:                 //mtr
            return d * 1000;
            break;
      default:
            return d;
            break;
   }
}

private double toRadian(doubleval)
{
     return (Math.PI / 180) * val;
}

1 comments:

Hi said...

ya it is working well ..nice post....

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger