Calculate distance between two Locations with Latitude and Longitude C#

Given that you have two set of Latitude and Longitude values, how do you calculate the distance between them?

For Windows Phone the below method is highly recommended since it used built-in library.

public double GetDistance(double latitude1, double longitude1, double latitude2, double longitude2)
     var firstCordinate = new GeoCoordinate(latitude1, longitude1);
     var secondCordinate = new GeoCoordinate(latitude2, longitude2);
     double distance = firstCordinate.GetDistanceTo(secondCordinate);
     return distance;

However if you need the deploy the same function across different platforms such as winrt, WPF and etc, the below code is more preferred since it is platform independent.

The following is known as the Haversine formula and is written in C#. More example of the formula in other language could be found at

public double Distance(double Latitude1, double Longitude1, double Latitude2, double Longitude2)
     double R = 6371.0;          // R is earth radius.
     double dLat = this.toRadian(Latitude2 - Latitude1);
     double dLon = this.toRadian(Longitude2 - Longitude1);
     double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(this.toRadian(Latitude1)) *   Math.Cos(this.toRadian(Latitude2)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
     double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
     double d = R * c;
     return d;
private double toRadian(double val)
    return (Math.PI / 180) * val;

Do note that there might be a slight different in the result obtained from the above methods due to the different in the R value being used.
Edited 1: Made some changes to the parameter of the second method to make it clearly.

Edited 2: It has come to my attention that GetDistanceTo method in GeoCoordinate uses 6376.5km thus accounting to the different in result between the two methods above. Source:

This entry was posted in Windows Phone, winrt and tagged , , , , , , , . Bookmark the permalink.

3 Responses to Calculate distance between two Locations with Latitude and Longitude C#

  1. Aria says:

    They don’t return the same value…


    • LZH says:

      May I know what are the results that you get from using the two different methods?
      I had amended to above method to fix the value of R which is the Radius of Earth in kilometer.
      If the different is small or insignificant, it might be due to the different in the R being used. I am not sure what is the value of R used in the method defined in the windows phone sdk.


    • LZH says:

      After doing some research, it seem that the GetDistanceTo method in GeoCoordinate uses 6376.5km as the Radius of earth. As a result, they are a different in the end result. Hopes this answer your concern.


Leave a Reply

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

You are commenting using your 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