Skip to content

Instantly share code, notes, and snippets.

@yasinkuyu
Created April 27, 2014 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasinkuyu/11353068 to your computer and use it in GitHub Desktop.
Save yasinkuyu/11353068 to your computer and use it in GitHub Desktop.
C# distance between two points (c# ile mesafe hesaplama)
// @yasinkuyu
// 27/04/2014
public static double Distance(double lat1, double lon1, double lat2, double lon2, char unit)
{
Func<double, double> deg2Rad = (x) => (x * (Math.PI / 180));
Func<double, double> rad2Deg = (x) => (x / Math.PI * 180.0);
var theta = lon1 - lon2;
var dist = Math.Sin(deg2Rad(lat1)) * Math.Sin(deg2Rad(lat2)) + Math.Cos(deg2Rad(lat1)) * Math.Cos(deg2Rad(lat2)) * Math.Cos(deg2Rad(theta));
dist = Math.Acos(dist);
dist = rad2Deg(dist);
dist = dist * 60 * 1.1515;
switch (unit)
{
case 'K':
dist = dist * 1.609344;
break;
case 'N':
dist = dist * 0.8684;
break;
}
return (dist);
}
Sample
Response.Write(Distance(32.9697, -96.80322, 29.46786, -98.53506, 'M') + " Miles<br>");
Response.Write(Distance(32.9697, -96.80322, 29.46786, -98.53506, 'K') + " Kilometers<br>");
Response.Write(Distance(32.9697, -96.80322, 29.46786, -98.53506, 'N') + " Nautical Miles<br>");
//Output
//262,677793805435 Miles
//422,738931394014 Kilometers
//228,10939614064 Nautical Miles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment