function :

Code:

public static Double measure(double lat1, double lon1, double lat2, double lon2) { // generally used geo measurement
 // function
 double R = 6378.137; // Radius of earth in KM
 double dLat = lat2 * Math.PI / 180 - lat1 * Math.PI / 180;
 double dLon = lon2 * Math.PI / 180 - lon1 * Math.PI / 180;
 double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180)
 * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
 double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
 double d = R * c;
 return d * 1000; // meters
 }


function in kotlin vertion :

Code:

fun measure(lat1:Double, lon1:Double, lat2:Double, lon2:Double):Double { // generally used geo measurement
  // function
  val R = 6378.137 // Radius of earth in KM
  val dLat = lat2 * Math.PI / 180 - lat1 * Math.PI / 180
  val dLon = lon2 * Math.PI / 180 - lon1 * Math.PI / 180
  val a = (Math.sin(dLat / 2) * Math.sin(dLat / 2) + (Math.cos(lat1 * Math.PI / 180)
                                                      * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2)))
  val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
  val d = R * c
  return d * 1000 // meters
}


use example : get the distance from Pripyat city to Chernobyl power plant  respectively:

System.out.println(measure(51.4045, 30.0542, 51.2763, 30.2219));

output : 18430.034150242464 about 18km // not great not terrible
:X_X: