Calculate Distance Between Geocodes in C# and JavaScript
There are times when I need code to calculate the distance between two geocodes (Lat/Long points). I don’t need it very often and until now I didn’t have a code snippet that I could jus copy and paste. I was searching and found Rob Conery’s LINQ and Geocoding post. It’s an interesting post and he includes a sample of how to use Lambda expressions in C# 3.0 to calculate the distance between two geocodes in Miles.
I decided to copy his code snippet and make sure it works in .NET 2.0 and also convert it to JavaScript. So below are both my C# 2.0 and JavaScript snippets for calculating the distance between two geocodes.
C# 2.0
Sample Usage:
// Calculate Distance in Milesdouble
d = GeoCodeCalc.CalcDistance(47.8131545175277, 122.783203125, 42.0982224111897, 87.890625);
// Calculate Distance in Kilometersdouble
d = GeoCodeCalc.CalcDistance(47.8131545175277, 122.783203125, 42.0982224111897, 87.890625, GeoCodeCalcMeasurement.Kilometers);
GeoCodeCalc C# Class:
public static class GeoCodeCalc{
public const double EarthRadiusInMiles = 3956.0;
public const double EarthRadiusInKilometers = 6367.0;
public static double ToRadian(double val) { return val * (Math.PI / 180); }
public static double DiffRadian(double val1, double val2) { return ToRadian(val2)  ToRadian(val1); }
/// <summary>
/// Calculate the distance between two geocodes. Defaults to using Miles.
/// </summary>
public static double CalcDistance(double lat1, double lng1, double lat2, double lng2) {
return CalcDistance(lat1, lng1, lat2, lng2, GeoCodeCalcMeasurement.Miles);
}
/// <summary>
/// Calculate the distance between two geocodes.
/// </summary>
public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, GeoCodeCalcMeasurement m) {
double radius = GeoCodeCalc.EarthRadiusInMiles;
if (m == GeoCodeCalcMeasurement.Kilometers) { radius = GeoCodeCalc.EarthRadiusInKilometers; }
return radius * 2 * Math.Asin( Math.Min(1, Math.Sqrt( ( Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
}
}
public enum GeoCodeCalcMeasurement : int
{
Miles = 0,
Kilometers = 1
}
JavaScript
Sample Usage:
// Calculate distance in Milesvar
d = GeoCodeCalc.CalcDistance(47.8131545175277, 122.783203125, 42.0982224111897, 87.890625, GeoCodeCalc.EarthRadiusInMiles);
// Calculate distance in Kilometersvar
d = GeoCodeCalc.CalcDistance(47.8131545175277, 122.783203125, 42.0982224111897, 87.890625, GeoCodeCalc.EarthRadiusInKilometers);
GeoCodeCalc JavaScript Class:
var GeoCodeCalc = {};
GeoCodeCalc.EarthRadiusInMiles = 3956.0;
GeoCodeCalc.EarthRadiusInKilometers = 6367.0;
GeoCodeCalc.ToRadian = function(v) { return v * (Math.PI / 180);};
GeoCodeCalc.DiffRadian = function(v1, v2) {
return GeoCodeCalc.ToRadian(v2)  GeoCodeCalc.ToRadian(v1);
};
GeoCodeCalc.CalcDistance = function(lat1, lng1, lat2, lng2, radius) {
return radius * 2 * Math.asin( Math.min(1, Math.sqrt( ( Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.cos(GeoCodeCalc.ToRadian(lat1)) * Math.cos(GeoCodeCalc.ToRadian(lat2)) * Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
};
Related Posts

Using the Popover API with HTML and Javascript in the browser
10 May 2024 
C#: How to Enumerate over Enum values and names
03 May 2024 
Get Started with Node.js Development
26 Apr 2024 
C#: Console App that Accepts CommandLine Arguments
23 Apr 2024 
C#: How to iterate over a dictionary?
20 Apr 2024 
C#: What is the difference between 'string' and System.String?
16 Apr 2024