1

我已经像这样开始了我的代码:

    public static void main(String[] args) {

//Intro 
    System.out.print("This program calculates the distance between two cities,     given their respective latitude and longitude points.");
    System.out.println();

//Get values for Latitude and Longitude
Scanner console = new Scanner(System.in);
    double lat1 = getCoordinates(" latitude 1: ", console); // Input lat1
    double long1 = getCoordinates(" longitude 1: ", console); // Input long1
    double lat2 = getCoordinates(" latitude 2: ", console); // Input lat2
    double long2 = getCoordinates(" longitude 2: ", console); // Input long2

public static double getCoordinates(String message, Scanner scan)
    {   
    System.out.print("Please enter" + message);
    int degrees = scan.nextInt();
    int minutes = scan.nextInt();
    return Math.toRadians(degrees + (minutes / 60.0));
    }

//Calculate Distance
public static double finalAnswer(double lat1, double long1, double lat2, double long2) {
double radiusOfEarth = 6372.795; //radius of Earth in km
double distance = Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
                  Math.cos(lat1)*Math.cos(lat2) *
                  Math.cos(long2-long1)) * radiusOfEarth;

return distance;
System.out.print("The distance between the two cities is approximately " + distance + "km");

}
}

我需要能够输入每个纬度和经度点的度数和分钟数(度数、分钟数)。但是,当我尝试在代码的最后一行打印“距离”时出现编译错误。此外,Eclipse 告诉我没有使用 lat1、long1、lat2 和 long2(“获取纬度和经度的值”部分)?有人可以帮我打印距离吗?谢谢你。

4

3 回答 3

3

在您的代码中:

..
return distance;
System.out.print("The distance between the two cities is approximately " 
    + distance + "km");
..

将其更改为:

..
System.out.print("The distance between the two cities is approximately " 
    + distance + "km");
return distance;
..

这是用Javascript 实现的用于计算距离的Haversine 公式。(应该很容易翻译成 Java。)

计算“大圆”距离的公式

坐标为 {lat1,lon1} 和 {lat2,lon2} 的两点之间的大圆距离 d 由下式给出:

d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))

一个数学上等价的公式是:

d=2*asin(sqrt((sin((lat1-lat2)/2))^2 + cos(lat1) cos(lat2) (sin((lon1-lon2)/2))^2))

于 2012-11-01T06:18:10.690 回答
0

你能请这些链接找出两个纬度和经度之间的距离吗

http://www.codeproject.com/Articles/12269/Distance-between-locations-using-latitude-and-long

否则还有另一个想法,您可以尝试使用谷歌地图API通过使用纬度和经度找出两个城市的距离

希望这可以帮助

于 2012-11-01T06:27:02.640 回答
0

以下代码可以计算给定经度和纬度的地方之间的距离。它使用haversine公式

import java.util.Scanner;
class haversine {
        double long1;
        double lat1;
        double long2;
        double lat2;
        haversine (Double long1, Double lat1, Double long2, Double lat2) {
                this.long1 = long1;
                this.lat1 = lat1;
                this.long2 = long2;
                this.lat2 = lat2;
        }
        double execute() {
                return Math.pow(Math.sin((lat2 - lat1)/2) ,2) + Math.cos(long1) * Math.cos(long2) * Math.pow(Math.sin((long2 - long1)/2) ,2);
        }
        double disCalc (Double exe) {
                Double radius_of_earth = 6378.1;
                return 2 * radius_of_earth * Math.asin(Math.pow(exe, 2));
        }

}
class test28 {
        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                System.out.println("longitude and latitude of home : ");
                double long1 = scan.nextDouble();
                double lat1 = scan.nextDouble();
                System.out.println("longitude and latitude of travel destination : ");
                double long2 = scan.nextDouble();
                double lat2 = scan.nextDouble();
                haversine obj = new haversine(long1, lat1, long2, lat2);
                double exe = obj.execute();
                double distance = obj.disCalc(exe);
                System.out.println("The Distance is : " + distance);
                scan.close();


        }
}
于 2020-04-19T14:06:48.963 回答