我知道如何开始,也知道如何放入扫描仪和所有物品,但是在学校,我从未真正了解过经度和纬度公式以及如何将这些点转换为弧度。因此,我对这个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语言”并将其插入我的程序?
您需要搜索的关键词是"Haversine formula"。
[一种易于理解的方法,但是对于小距离而言不太准确,是回想起可以使用点积来计算两个向量A
和B
之间的角度:
A⋅B = | A | * | B | * cos(theta)
因此,如果将极数纬度/经度对转换为3D笛卡尔坐标(是的,则需要使用Math.toRadians()
,Math.cos()
和Math.sin()
来完成,然后计算点积,然后将获得cos(theta)
,因此使用Math.acos()
获得theta
。
然后您可以简单地以D = R * theta
来计算距离,其中R
是地球的半径,而theta
保持弧度。
我建议阅读有关WGS84的更多信息。
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();
}
}
您可以在此链接中查找逻辑。
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数据库中获取价值:
Calculate distance given 2 points, latitude and longitude
我试图创建一个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;
}