【问题标题】:Android Location.distanceToAndroid Location.distanceTo
【发布时间】: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


【解决方案1】:

不,距离永远不会是负数!

对于南方运动,您可以扩展您的代码:

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

虽然我建议将距离和南向运动分开(将来也许您也想检测东西向运动)

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多