所以我有Location类和一个radio类(radio类包含坐标)。基本上对于我正在编写的程序,我需要一个位置来获取无线电类的坐标,并使用距离公式计算两组坐标。
位置:
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());
}
}
我还应该提到main方法在广播中的唯一原因是因为我只是在测试是否一切正常,任何帮助或建议都将不胜感激。
非常感谢你们!
最佳答案
根据对ward的建议的推断,我建议执行以下操作:
位置类别:
public class Location {
// private data
// constructor
public double distance(Location otherLoc) {
// Use distance formula here
}
}
无线电课:
public class Radio {
private Location loc;
public double distance(Radio otherRadio) {
return loc.distance(otherRadio.loc);
}
}
如果这不能解决您的所有问题,请在上方编辑您的信息,或发布包含其他详细信息的新信息,以便我们提供帮助。
关于java - 程序涉及使用距离功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11786514/