我如何编写一个Java程序来计算地球上两点之间的距离?

我如何编写一个Java程序来计算地球上两点之间的距离?,java,math,latitude-longitude,radians,earthdistance,Java,Math,Latitude Longitude,Radians,Earthdistance,我知道如何开始,我知道如何安装扫描仪,但在学校,我从来没有真正学习过经度和纬度公式,以及如何将这些点转换为弧度。所以我几乎被困在这个Java问题上。以下是我到目前为止的情况: import java.util.*; class DistanceCalculator { // Radius of the earth in km; this is the class constant. public static final double Radius = 6372.795;

我知道如何开始,我知道如何安装扫描仪,但在学校,我从来没有真正学习过经度和纬度公式,以及如何将这些点转换为弧度。所以我几乎被困在这个Java问题上。以下是我到目前为止的情况:

import java.util.*;

class DistanceCalculator {

    // Radius of the earth in km; this is the class constant.
    public static final double Radius = 6372.795; 

    /**
     * This program computes the spherical distance between two points on the surface of the Earth.
     */

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        intro();

        System.out.print("Longitude (degrees.minutes) ");
        double Longitude = console.nextDouble();
        System.out.print("Latitude (degrees.minutes) ");
        double Latitude = console.nextDouble(); 
    }

    public static double distFrom(double lat1, double lng1, double lat2, double lng2); 
        double Latitude = Math.toRadians(...);  
    }

    public static void intro() {
        System.out.println("This program computes the spherical distance between two points on the surface of the Earth.");
        System.out.println("\tPlease start by entering the longitude and the latitude of location 1.");
    }
}
在JavaIDE中,他们说经度和纬度点(在
intro();
下面的点)没有被使用,我知道原因,因为我还没有真正定义它们。 我知道我缺少经度和纬度的公式。在我的书中,它想让我使用余弦球面定律,因为我在学校从来没有学过这一点,无论我从我找到的网站上多么努力地学习这个公式,我都不知道如何将它转换成Java语言。
另一个问题是,如何将经度/纬度点的度数和分钟转换为弧度?我一定要用
数学.toRadians
吗?哦,是的,还有,我的答案必须是公里


更新:你们中的一些人谈论的数学函数让我非常困惑。在学校(我是一名高中生),即使是在数学IB SL,我的老师也从未教过我们如何找到长/纬度点……至今。所以我很难理解。既然余弦球面定律公式是在线的,我基本上就是把这个公式转换成“java语言”并插入我的程序吗?

你需要搜索的关键词是

一种更容易理解的方法,但对于小距离来说并不十分准确,是回忆两个向量
A
B
之间的角度可以使用点积计算:

A⋅ B=| A |*| B |*cos(θ)

因此,如果你将极坐标对转换为三维笛卡尔坐标(是的,你需要使用
Math.toRadians()
Math.cos()
Math.sin()
来实现这一点,然后计算点积,你将得到
cos(theta)
,所以使用
Math.acos()
来得到
theta


然后,您可以简单地计算出距离,即
D=R*theta
,其中
R
是地球的半径,
theta
保持弧度。

您可以查看此链接了解逻辑

PHP中的函数…我不懂Java。因此有人编辑了我的帖子。以下是PHP函数:

function getDistanceBetweenPointsNew($latitude1, $longitude1,
$latitude2, $longitude2, $unit = 'Mi')
{
    $theta = $longitude1 - $longitude2;
    $distance = (sin(deg2rad($latitude1)) *
    sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) *
    cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
    $distance = acos($distance);
    $distance = rad2deg($distance);
    $distance = $distance * 60 * 1.1515;
    switch($unit)
    {
        case 'Mi': break;
        case 'Km' : $distance = $distance *1.609344;
    }
    return (round($distance,2));
}
还要从MySQL数据库中获取值:

我试图创建一个java函数,我不知道它是否有效

试试这个。如果有人能帮忙,试着编辑我的java代码

import java.math.BigDecimal;

public static double round(double unrounded, int precision, int roundingMode)
{
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}

public static double distFrom(double lat1, double lng1, double lat2, double lng2, String unit)
{
    double theta = lng1 - lng2;

    double distance = (
        Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2))
     )+(
        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta))
     );
    distance = Math.acos(distance);
    distance = Math.toDeg(distance);
    distance = distance * 60 * 1.1515;

    switch(unit)
    {
        /* Mi = miles, Km = Kilometers */
        case "Mi"   : 
            break;
        case "Km"   : 
            distance = distance *1.609344;
            break;
    }
    distance = round(distance, 2, BigDecimal.ROUND_HALF_UP);
    return distance;
}

我建议你多读一些关于你的书


}

我的书要求学习余弦球面定律?谢谢,但是A.B代表什么?这就像某个点的弧度A,B是第二距离吗?如果你不能理解最琐碎的数学公式,你通过这项测试的机会为零。请让你的导师解释一下。你能解释一下PHP是什么吗?我想知道“我从来没听说过这个词。你可以在谷歌上搜索……在尝试研究它之前,不要评论任何类似的东西。不管怎样,php是一种“服务器端脚本语言”参考:让我试着用“Java”来实现这个功能”.Oops,很抱歉。谢谢你的解释。最好不要,因为实际上你刚刚得到了家庭作业的答案。这就是所谓的“作弊”。好的答案还应该包含一些解释,而不仅仅是代码。
     import java.util.*;

       public class SphericalDistance {
public static void main(String[] args){
System.out.println(" This program computes the spherical distance\n between two points, 1 and 2.");
System.out.println(" Please enter the latitude and longitude for \n each point as a pair of integers, degrees \n followed by minutes:");
System.out.print("Latitude 1:");
    Scanner s=new Scanner(System.in);
    double latangledeg = s.nextDouble();
    double latanglemin = s.nextDouble()/60;

    double phideg = latangledeg + latanglemin;
    double phi1 = phideg * Math.PI/180;


    System.out.print("Longitude 1:");

    double lonangledeg = s.nextDouble();
    double lonanglemin = s.nextDouble()/60;

    double lambdadeg = lonangledeg + lonanglemin;
    double lambda1 = lambdadeg * Math.PI/180;


    System.out.println("Latitude 2:");

    double latangledeg2 = s.nextDouble();
    double latanglemin2 = s.nextDouble()/60;

    double phideg2 = latangledeg2 + latanglemin2;
    double phi2 = phideg2 * Math.PI/180;


    System.out.println("Longitude 2:");

    double lonangledeg2 = s.nextDouble();
    double lonanglemin2 = s.nextDouble()/60;

    double lambdadeg2 = lonangledeg2 + lonanglemin2;
    double lambda2 = lambdadeg2 * Math.PI/180;


    double lambdaf = lambda2 - lambda1;
    double angdistance = Math.acos(Math.sin(phi1)*Math.sin(phi2) + Math.cos(phi1)*Math.cos(phi2)*Math.cos(lambdaf));
    System.out.println("Angular Distance = " + angdistance + " radians");
    int distancekm = (int)(angdistance * 6372.795);
    int distancemi = (int) (distancekm * .621371);
    System.out.println("Distance         = " + distancekm + " kilometers");
    System.out.println("Distance         = " + distancemi + " miles");

    s.close();
}