using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Utility
{
    public class Distance
    {
        public double CalculateDistance(double lat1, double lat2, double lon1, double lon2)
        {
            lon1 = GetRadian(lon1);
            lon2 = GetRadian(lon2);
            lat1 = GetRadian(lat1);
            lat2 = GetRadian(lat2);

            // Haversine formula
            double dlon = lon2 - lon1;
            double dlat = lat2 - lat1;
            double a = Math.Pow(Math.Sin(dlat / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2), 2);

            double c = 2 * Math.Asin(Math.Sqrt(a));

            // Radius of earth in
            // kilometers. Use 3956
            // for miles
            double r = 6371;
            double result = (c * r);
            // calculate the result
            return (System.Math.Round(result, 2));
        }
        private double GetRadian(double angleIn10thofaDegree)
        {
            // Angle in 10th
            // of a degree
            return (angleIn10thofaDegree * Math.PI) / 180;
        }
    }
}
