考虑到纬度和经度坐标,我如何打印两个城市之间的距离?

时间:2012-11-01 06:04:10

标签: java eclipse distance latitude-longitude

我已经开始使用这样的代码:

    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(“获取纬度和经度值”部分)?有人可以帮我打印距离吗?谢谢。

3 个答案:

答案 0 :(得分: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;
..

这是计算距离的Haversine-formula implemented in Javascript。 (应该很容易翻译成Java。)

Formulas for calculating 'great circle' distances

  

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

     

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))

答案 1 :(得分:0)

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

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

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

希望这有帮助

答案 2 :(得分: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();


        }
}