Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. public class GeoLocation
  2. {
  3. public static final double RADIUS = 3963.1676;
  4.  
  5. public static final double KM_PER_MILE = 1.60934;
  6.  
  7. private double latitude;
  8. private double longitude;
  9.  
  10. public GeoLocation(double theLatitude, double theLongitude)
  11. {
  12. latitude = theLatitude;
  13. longitude = theLongitude;
  14. }
  15. public double getLatitude()
  16. {
  17. return latitude;
  18. }
  19. public double getLongitude()
  20. {
  21. return longitude;
  22. }
  23. public String toString()
  24. {
  25. return "latitude: " + latitude + ", longitude: " + longitude;
  26. }
  27. public double distanceFrom(GeoLocation other)
  28. {
  29. double lat1 = Math.toRadians(latitude);
  30. double long1 = Math.toRadians(longitude);
  31. double lat2 = Math.toRadians(other.latitude);
  32. double long2 = Math.toRadians(other.longitude);
  33. double theCos = Math.sin(lat1) * Math.sin(lat2) +
  34. Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
  35. double arcLength = Math.acos(theCos);
  36. double result = arcLength * RADIUS;
  37. return result;
  38. }
  39. public double distanceFromInKilometers(GeoLocation other)
  40. {
  41. double lat1 = Math.toRadians(latitude);
  42. double long1 = Math.toRadians(longitude);
  43. double lat2 = Math.toRadians(other.latitude);
  44. double long2 = Math.toRadians(other.longitude);
  45. double theCos = Math.sin(lat1) * Math.sin(lat2) +
  46. Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
  47. double arcLength = Math.acos(theCos);
  48. double result = arcLength * RADIUS;
  49. double result2 = result * KM_PER_MILE;
  50. return result2;
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement