1

2 つの 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);
           }


       }

それは正しいと思いますか?

4

1 に答える 1

4

いいえ、距離が負になることはありません!

南への移動の場合、コードを拡張できます:

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

距離と南の動きを分離することをお勧めしますが(将来的には東西の動きも検出したいかもしれません)

于 2013-05-28T20:17:17.587 に答える