LocationUtil.cs 956 B

12345678910111213141516171819202122232425262728293031323334
  1. using UnityEngine;
  2. using System;
  3. namespace Immersal.Samples.Util
  4. {
  5. public static class LocationUtil
  6. {
  7. private static double toRadians(double angle)
  8. {
  9. return (Math.PI / 180) * angle;
  10. }
  11. // Haversine distance
  12. public static double DistanceBetweenPoints(Vector2 p1, Vector2 p2)
  13. {
  14. double R = 6371e3; // Earth's radius in metres
  15. double radLat1 = toRadians(p1.x);
  16. double radLat2 = toRadians(p2.x);
  17. double radLon1 = toRadians(p1.y);
  18. double radLon2 = toRadians(p2.y);
  19. double deltaLat = radLat2 - radLat1;
  20. double deltaLon = radLon2 - radLon1;
  21. // the square of half the chord length between the points
  22. double a = Math.Sin(deltaLat / 2) * Math.Sin(deltaLat / 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Sin(deltaLon / 2) * Math.Sin(deltaLon / 2);
  23. // angular distance in radians
  24. double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a));
  25. // distance in metres
  26. double d = R * c;
  27. return d;
  28. }
  29. }
  30. }