【发布时间】:2013-05-28 20:13:45
【问题描述】:
我写了一个函数来确定两个 GPS 位置之间的距离。
public float latDistance(Location newLocal){
// get distance
Location tempLocal1 = new Location("ref1");
Location tempLocal2 = new Location("ref2");
// get lon difference
tempLocal1.setLatitude(local.getLatitude());
tempLocal1.setLongitude(0);
tempLocal1.setAltitude(0);
tempLocal2.setLatitude(newLocal.getLatitude());
tempLocal2.setLongitude(0);
tempLocal2.setAltitude(0);
return tempLocal2.distanceTo(tempLocal1);
}
我的问题是,这会返回负值吗?我的目标是获得一个反映他们是向北还是向南移动的距离。所以如果他们从起始位置向南移动,我想要一个负值,如果向北是正值?
似乎我总是得到一个正数,但我不知道这是否只是我的 gps 读数不准确
编辑:
my code now looks like this.. and i know it irregular to ask people to comment on the logic, but its a difficult thing to test as it relies on a gps signal and to test i have to basically go out side and get a good signal, which pulls me away from my IDE and LogCat..
public float getLattitudeDistance(Location newLocal){
// get distance
Location tempLocal1 = new Location("ref1");
Location tempLocal2 = new Location("ref2");
// get lon difference
tempLocal1.setLatitude(local.getLatitude());
tempLocal1.setLongitude(0);
tempLocal1.setAltitude(0);
tempLocal2.setLatitude(newLocal.getLatitude());
tempLocal2.setLongitude(0);
tempLocal2.setAltitude(0);
if(local.getLatitude()>newLocal.getLatitude()){
return -tempLocal2.distanceTo(tempLocal1);
}else{
return tempLocal2.distanceTo(tempLocal1);
}
}
public float getLongitudeDistance(Location newLocal){
// get distance
Location tempLocal1 = new Location("ref1");
Location tempLocal2 = new Location("ref2");
// get lon difference
tempLocal1.setLatitude(0);
tempLocal1.setLongitude(local.getLongitude());
tempLocal1.setAltitude(0);
tempLocal2.setLatitude(0);
tempLocal2.setLongitude(newLocal.getLongitude());
tempLocal2.setAltitude(0);
if(local.getLongitude()>newLocal.getLongitude()){
return -tempLocal2.distanceTo(tempLocal1);
}else{
return tempLocal2.distanceTo(tempLocal1);
}
}
看起来对吗?
【问题讨论】:
-
纬度码有效,经度错误,不能设置为0,两个纬度值都设置为local.getLatitude()) 原因:纬度影响经度距离,因为北移时经度距离缩小(提高纬度)
-
测试:但是您仍然可以创建一个位置对象并为您的代码创建一个 Junit 测试,强烈建议这样做。
-
你把我弄丢了。你能用代码告诉我你在说什么吗?将 getLongitudeDistance 中的 lat 值设置为零但 getLattitudeDistance 不是这样有什么问题?
-
另外,如果我想测量海拔差怎么办?将 lats 和 longs 都设置为零可以吗?
-
海拔以米为单位。你可以直接Substrat。 DustanceTo 不使用海拔高度。距离始终是 2d 距离。
标签: gps latitude-longitude android-location