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();) は使用されていないと彼らは言いますが、その理由はわかっています。まだ実際に定義していないからです。経度と緯度の式が不足していることはわかっています。私の本では、余弦の球面法則を使用するように求められています。学校でこれを学んだことがないので、検索した Web サイトから公式をどれだけ勉強しても、それを転送する方法がわかりません。 Java言語に。
別の問題は、経度/緯度のポイントから度と分をラジアンに変換するにはどうすればよいですか? 私はMath.toRadians物を使わなければなりませんか?そうそう、私の答えはキロメートルでなければなりません。

更新: 何人かが話している数学関数は、私を大いに混乱させます。学校では (私は高校生です)、Math IB SL でさえ、私の先生は長緯度の見つけ方を教えてくれませんでした。ポイント...まだ。だから、私は把握するのが難しいです。コサインの球面法則の公式がオンラインになっているので、基本的にはその公式を「Java言語」に変換してプログラムにプラグインするだけですか?

4

4 に答える 4

4

検索する必要があるキーワードは、「Haversine formula」です。

理解しやすい方法ですが、距離が短い場合はそれほど正確ではありませんが、2 つのベクトル間の角度は内積を使用して計算できることを思い出してAくださいB

A ⋅ B = |A| * |B| * cos(シータ)

したがって、極緯度/経度のペアを 3D デカルト座標に変換すると (そうです、 を使用する必要がありMath.toRadians()Math.cos()そのためには を使用しMath.sin()、内積を計算すると、 が得られるcos(theta)ので、 を使用Math.acos()して を取得しますtheta

は地球の半径であり、D = R * thetaラジアンのままです。Rtheta

于 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 に答える