@Test public void invalidLatitudeTest() { double invalidLat = Double.NaN; double distance = DistanceCalculation.distanceBetween(invalidLat, lon1, lat2, lon2); assertEquals(Double.NaN, distance, 1e-6); }
public static double distanceBetween(double lat1, double lon1, double lat2, double lon2) { double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lon2 - lon1); lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); return 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); }
DistanceCalculation { public static double distanceBetween(double lat1, double lon1, double lat2, double lon2) { double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lon2 - lon1); lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); return 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); } }
DistanceCalculation { public static double distanceBetween(double lat1, double lon1, double lat2, double lon2) { double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lon2 - lon1); lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); return 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); } private  DistanceCalculation(); }
DistanceCalculation { public static double distanceBetween(double lat1, double lon1, double lat2, double lon2) { double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lon2 - lon1); lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); return 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); } private  DistanceCalculation(); static double distanceBetween(double lat1, double lon1, double lat2, double lon2); static double distanceBetweenInKm(double lat1, double lon1, double lat2, double lon2); }
DistanceCalculation { public static double distanceBetween(double lat1, double lon1, double lat2, double lon2) { double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lon2 - lon1); lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); return 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); } private  DistanceCalculation(); static double distanceBetween(double lat1, double lon1, double lat2, double lon2); static double distanceBetweenInKm(double lat1, double lon1, double lat2, double lon2);  }