所以我有 Location 类和一个无线电类(无线电类包含坐标)。基本上,对于我正在编写的程序,我需要从无线电类中获取坐标的位置,并使用距离公式来计算两组坐标。地点:
public class Location {
private double lat, lon;
public Location (double lat, double lon){
this.lat=lat;
this.lon=lon;
}
public Location (){
this.lat=0.0;
this.lon=0.0;
}
public void setLat(double lat){
this.lat=lat;
}
public void setLon(double lon){
this.lon=lon;
}
public double Distance (double lat1, double lon1, double lat2, double lon2) {
lat1 = Math.toRadians(lat1);
lon1 = Math.toRadians(lon1);
lat2 = Math.toRadians(lat2);
lon2 = Math.toRadians(lon2);
double cosLat2 = Math.cos(lat2);
double sinLonChange = Math.sin(lon2-lon1);
double cosLat1 = Math.cos(lat1);
double sinLat2 = Math.sin(lat2);
double sinLat1 = Math.sin(lat1);
double cosLonChange = Math.cos(lon2-lon1);
double a = Math.pow((cosLat2*sinLonChange), 2);
double b = Math.pow(((cosLat1*sinLat2)-(sinLat1*cosLat2*cosLonChange)), 2);
double c = (sinLat1*sinLat2)+(cosLat1*cosLat2*cosLonChange);
double spherDistance = Math.atan(Math.sqrt(a+b)/c);
double Distance = 3959 * spherDistance;
return Distance;
}
public double getLat(){
return lat;
}
public double getLon(){
return lon;
}
public double getDistance(){
return Distance;
}
public String toString(){
String dist="lat"+lat+"lon"+lon;
return dist;
}
}
和收音机:
public class Radio {
public static void main(String[] args) {
Location loc1=new Location(10,20);
Location loc2=new Location(30,40);
System.out.println(loc1.toString());
System.out.println(loc2.toString());
}
}
我还应该提到,主要方法在无线电中的唯一原因是因为我只是在测试以查看是否一切正常。任何帮助或建议将不胜感激。非常感谢大家!