C#.NET Articles

KB: Distance between two points on globe - Calculation using C#

SUNDAY, MARCH 28, 2010

The Haversine formula is used for calculating the distance between two points on the globe. This function gives a nearly accurate distance value, and is used widely in GPS related calculations and for vehicle tracking. This article provides a code snippet for calculating distance between two points taking latitude and longitude values as example.

This article is a sequel to the previous article on the same topic, but using T-SQL for calculation - Calculate distance between two points on globe from latitude and longitude coordinates. Please refer to this article for details of the Haversine formula and distance calculation.

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;
    }

The datatype used for all variables is double. The latitude and longitude values also expressed as double typed, so it requires conversion of the original coordinate value in degrees into double datatypes.

Double value = degree + minute / 60 + seconds / 3600

ConsultSarath - We provide end to end outsourcing solutions for .net programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for php programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for python programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India.

Other Popular Articles
We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...