Gps Android Location.distanceTo

Gps Android Location.distanceTo,gps,latitude-longitude,android-location,Gps,Latitude Longitude,Android Location,我编写了一个函数来确定两个GPS位置之间的距离 public float latDistance(Location newLocal){ // get distance Location tempLocal1 = new Location("ref1"); Location tempLocal2 = new Location("ref2"); // get lon difference tem

我编写了一个函数来确定两个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读数

编辑:


这看起来对吗?

不,距离从来都不是负的

对于南移,您可以扩展代码:

float distance =  tempLocal2.distanceTo(tempLocal1);
// lat1: previous latitude
// lat2: current latitude
if (lat2 < lat1) {
  // movement = south
  distance = -distance:
} else {
  // movement = north or parallel aeqator or not moving
}
return distance

虽然我建议将来将距离和南部运动分开,但也许你也希望检测到东西方向的运动

不,距离从来都不是负的

对于南移,您可以扩展代码:

float distance =  tempLocal2.distanceTo(tempLocal1);
// lat1: previous latitude
// lat2: current latitude
if (lat2 < lat1) {
  // movement = south
  distance = -distance:
} else {
  // movement = north or parallel aeqator or not moving
}
return distance

虽然我建议将来分离距离和南移,但也许您希望检测东西移动,纬度代码也会起作用,经度是错误的,您不应设置为0,将两个纬度值都设置为本地。getLatitude原因:纬度影响经度距离,因为当向北移动时,纵向距离会缩短,提高纬度测试:但是你仍然可以为你的代码创建一个位置对象和一个Junit测试,这是强烈建议的。你失去了我。。你能用密码告诉我你在说什么吗?将getLongitudeDistance中的lat值设置为零有什么错,但getLattitudeDistance的情况不是这样的?另外,如果我想测量高差,将lats和longs全部设置为零可以吗?高度是以米为单位测量的。你可以直接说。DustanceTo不使用海拔高度。距离始终是2d距离。纬度代码将起作用,经度错误,不应设置为0,将两个纬度值都设置为local。getLatitude原因:纬度影响经度距离,因为当向北移动时,纵向距离会缩短,提高纬度测试:但是你仍然可以为你的代码创建一个位置对象和一个Junit测试,这是强烈建议的。你失去了我。。你能用密码告诉我你在说什么吗?将getLongitudeDistance中的lat值设置为零有什么错,但getLattitudeDistance的情况不是这样的?另外,如果我想测量高差,将lats和longs全部设置为零可以吗?高度是以米为单位测量的。你可以直接说。DustanceTo不使用海拔高度。该距离始终是二维距离。