Popular Posts

Yatharth Sharma the coolest guy on the planet welcomes you all. please keep visiting for updates and don't forget to send in your suggestions....

Friday, December 13, 2013

Calculate distance between two points on globe from latitude and longitude coordinates.


The C# implementation of the Haversine formula is:

 public double GetDistanceBetweenPoints(double lat1, double long1, double lat2, double long2)
    {
        double distance = 0;
       
        double dLat = (lat2 - lat1) / 180* Math.PI;
        double dLong = (long2 - long1) / 180 * Math.PI;
 
        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2)
                    + Math.Cos(lat2) * Math.Sin(dLong/2) * Math.Sin(dLong/2);
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
       
        //Calculate radius of earth
        // For this you can assume any of the two points.
        double radiusE = 6378135; // Equatorial radius, in metres
        double radiusP = 6356750; // Polar Radius
 
        //Numerator part of function
        double nr = Math.Pow(radiusE * radiusP * Math.Cos(lat1 / 180 * Math.PI), 2);
        //Denominator part of the function
        double dr = Math.Pow(radiusE * Math.Cos(lat1 / 180 * Math.PI), 2)
                        + Math.Pow(radiusP * Math.Sin(lat1 / 180 * Math.PI), 2);
        double radius = Math.Sqrt(nr / dr);
 
        //Calaculate distance in metres.
        distance = radius * c;
        return distance;
    }

1 comment:

  1. Nice statistics which are updated up to the latest. Can I see the past statistics? Would like to compare them. distance-between.info

    ReplyDelete