0

我知道如何开始,我知道如何放入扫描仪和所有东西,但在学校里,我从来没有真正了解经度和纬度公式以及如何将这些点转换为弧度。所以我几乎被困在这个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.");
    }
}

在 Java IDE 中,他们说intro();没有使用经度和纬度点(下面的),我知道为什么,因为我还没有真正定义它们。我知道我错过了经度和纬度的公式。在我的书中,它要我使用余弦的球面定律,由于我在学校从未学过这个,所以无论我如何从我寻找的网站上学习这个公式,我都不知道如何转移它成Java语言。
另一个问题是,如何将度数和分钟数从经度/纬度点转换为弧度?我必须使用Math.toRadians东西吗?哦,是的,而且,我的答案必须以公里为单位。

更新:你们中的一些人正在谈论的数学函数让我非常困惑。在学校(我是一名高中生),甚至在 Math IB SL,我的老师也从未教过我们如何找到经度/纬度。点...然而。所以我很难掌握。由于余弦公式的球面定律在线,我基本上只是将那个公式转换为“java语言”并将其插入我的程序吗?

4

4 回答 4

4

您需要搜索的关键词是“Haversine 公式”

一种更容易理解但对于小距离不太准确的方法是回忆两个向量之间的角度AB可以使用点积计算:

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

因此,如果您将极地纬度/经度对转换为 3D 笛卡尔坐标(是的,您需要使用Math.toRadians(),Math.cos()Math.sin()执行此操作,然后计算点积,然后您将得到cos(theta),因此使用Math.acos()来获取theta.

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

于 2012-10-28T08:17:26.590 回答
0

我建议阅读更多关于WGS84的信息。

这里有数学解释。

于 2012-10-28T09:08:23.397 回答
-1

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

http://aravindtrue.wordpress.com/2009/06/30/calculate-distance-using-latitude-and-longitude-php-mysql/

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 数据库中获取价值:

计算给定2点,纬度和经度的距离

我试图创建一个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;
}
于 2012-10-28T08:39:09.340 回答
-1
     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();
}

}

于 2019-09-27T18:16:41.787 回答