我有用于飞行管理系统的 Javafx 应用程序,我必须计算机场之间的距离,例如从杜塞尔多夫国际机场到科隆机场,我尝试了从此处的帖子中找到的 Geodys api,但它没有给我所需的值。有人可以给我指点这个问题吗?我从数据库中获取了纬度和经度。
这是 Api 代码:
public double distanceCalculator() throws SQLException {
Statement disanceState = connection.createStatement();
ResultSet startFlughafen = disanceState
.executeQuery("select * from flughaefen where name =\"" + startAirport.getValue() + "\"");
while (startFlughafen.next()) {
longitudeStart = startFlughafen.getDouble("lon");
latidudeStart = startFlughafen.getDouble("lat");
}
ResultSet zielFlughafen = disanceState
.executeQuery("select * from flughaefen where name =\"" + targetAirport.getValue() + "\"");
while (zielFlughafen.next()) {
longitudeZiel = zielFlughafen.getDouble("lon");
latidudeZiel = zielFlughafen.getDouble("lat");
}
GeodeticCalculator geoCalc = new GeodeticCalculator();
Ellipsoid reference = Ellipsoid.WGS84;
GlobalPosition startFlughafenValues = new GlobalPosition(latidudeStart, longitudeStart, 0.0); // Point A
GlobalPosition zielFlughafenValues = new GlobalPosition(latidudeZiel, longitudeZiel, 0.0); // Point B
distanceSet = geoCalc.calculateGeodeticCurve(reference, startFlughafenValues, zielFlughafenValues)
.getEllipsoidalDistance(); // Distance between Point A and Point B
return distanceSet;
}
这是我尝试过的另一个公式:
public double distanceCalculator() throws SQLException {
Statement disanceState = connection.createStatement();
ResultSet startFlughafen = disanceState
.executeQuery("select * from flughaefen where name =\"" + startAirport.getValue() + "\"");
while (startFlughafen.next()) {
longitudeStart = startFlughafen.getDouble("lon");
latidudeStart = startFlughafen.getDouble("lat");
}
ResultSet zielFlughafen = disanceState
.executeQuery("select * from flughaefen where name =\"" + targetAirport.getValue() + "\"");
while (zielFlughafen.next()) {
longitudeZiel = zielFlughafen.getDouble("lon");
latidudeZiel = zielFlughafen.getDouble("lat");
}
final int R = 6371; // Radius of the earth
double latDistance = Math.toRadians(latidudeZiel - latidudeStart);
double lonDistance = Math.toRadians(longitudeZiel - longitudeStart);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(longitudeStart)) * Math.cos(Math.toRadians(longitudeZiel))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
distances = R * c ; // convert to meters
distances = Math.pow(distances, 2) ;
return Math.sqrt(distances);
最佳答案
有几个库可以做到这一点,但在幕后他们也经常使用 GeographicLib: https://geographiclib.sourceforge.io/可以在文本中找到 Java 版本的链接。您可能正在寻找的类称为测地线。
关于Java 地理位置计算 - 没有得到正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59196084/