Assignment for Location Calculation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

  1. using System;
  2. namespace POCDistance.Util
  3. {
  4. public static class CalculateDistance
  5. {
  6. static double toRadians(
  7. double angleIn10thofaDegree)
  8. {
  9. // Angle in 10th
  10. // of a degree
  11. return (angleIn10thofaDegree * Math.PI) / 180;
  12. }
  13. public static double GetDistance(double lat1, double lon1, double lat2, double lon2)
  14. {
  15. // The math module contains
  16. // a function named toRadians
  17. // which converts from degrees
  18. // to radians.
  19. lon1 = toRadians(lon1);
  20. lon2 = toRadians(lon2);
  21. lat1 = toRadians(lat1);
  22. lat2 = toRadians(lat2);
  23. // Haversine formula
  24. double dlon = lon2 - lon1;
  25. double dlat = lat2 - lat1;
  26. double a = Math.Pow(Math.Sin(dlat / 2), 2) +
  27. Math.Cos(lat1) * Math.Cos(lat2) *
  28. Math.Pow(Math.Sin(dlon / 2), 2);
  29. double c = 2 * Math.Asin(Math.Sqrt(a));
  30. // Radius of earth in
  31. // kilometers. Use 3956
  32. // for miles
  33. double r = 6371;
  34. // calculate the result
  35. return (c * r);
  36. }
  37. }
  38. }