battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


battle programming a forum for elite programmers with extreme will power to sharpen theire skills
 
HomeSearchRegisterLog in
 

 java convert coordinates to meters

Go down 
AuthorMessage
kurosen
codding
codding
kurosen

Posts : 222
Join date : 2012-04-17

java convert coordinates to meters Empty
PostSubject: java convert coordinates to meters   java convert coordinates to meters EmptyMon Mar 23, 2020 10:16 am

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:
Back to top Go down
 
java convert coordinates to meters
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: