in region ? function

  • 0 Replies
  • 43 Views
*

yotamarker

  • Trusty Member
  • ********
  • Replicant
  • *
  • 729
    • battle programming
in region ? function
« on: March 23, 2020, 08:20:13 AM »
function :

Code: [Select]
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: [Select]
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:

 


Users Online

9 Guests, 1 User
Users active in past 15 minutes:
LOCKSUIT
[Trusty Member]

Most Online Today: 14. Most Online Ever: 340 (March 26, 2019, 09:47:57 PM)

Articles