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

Loop through JavaScript Arrays using for, forEach, and map functions
01 Jul 2020 
JavaScript: Convert String to Number
06 Apr 2020 
Basics of JavaScript Class Inheritance
02 Jan 2020 
Call Functions in JavaScript
24 Jul 2019
Recent Posts

Loop through JavaScript Arrays using for, forEach, and map functions
01 Jul 2020 
JavaScript: Convert String to Number
06 Apr 2020 
Encryption and Code are Greater Than Guns and Bombs
11 Mar 2020 
macOS: Monitor CPU Usage and Frequency Info
15 Feb 2020 
Basics of JavaScript Class Inheritance
02 Jan 2020
Recent on Build5Nines.com
Legal Disclaimer
This site is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. We also participates in affiliate programs with Udemy, and other sites. This site is compensated for referring traffic and business to these companies.