2013-12-20 49 views
0

我有一個Android應用程序,它可以計算出行走時從GPS的距離。當我開始申請並直行時,它會給出正確的距離,但是當我回到起點時,距離會減小。最後它顯示0,當我達到原來的位置。Android GPS距離ZERO,當回到開始位置

所以,我的問題是我怎樣才能完美地計算我的每一步距離。

這裏是我的活動:

public class Gps extends Activity { 
TextView display; 
Button start; 
boolean doubleclick = false; 
AnimationDrawable AppNameAnimation; 
private boolean enabled; 
double currentLon = 0; 
double currentLat = 0; 
double lastLon = 0; 
double lastLat = 0; 
double distance; 
Location loc; 
Animation animRotate; 
TextView startStop; 


LocationManager lm; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_test); 
    startStop = (TextView) findViewById(R.id.textView2); 
    display = (TextView) findViewById(R.id.textView1); 
    start = (Button) findViewById(R.id.button1); 
    animRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate); 

    start.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (!doubleclick) { 
       lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
       enabled = lm 
         .isProviderEnabled(LocationManager.GPS_PROVIDER); 
       if (!enabled) { 
        Intent inte = new Intent(
          Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(inte); 
       } else { 
        lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, 
          Loclist); 
        Log.e("successss", "111111111111"); 
       } 
      } else { 
       lm.removeUpdates(Loclist); 
       startStop.setText("Start"); 
       v.clearAnimation(); 
       doubleclick = false; 
       Intent in = new Intent(Gps.this, NextActivity.class); 
       // in.putExtra("distance", display.toString()); 
       startActivity(in); 
       finish(); 
      } 

     } 
    }); 
} 

LocationListener Loclist = new LocationListener() { 

    @Override 
    public void onLocationChanged(Location location) { 

     // TODO Auto-generated method stub 
     Log.e("successss", "2222222222222"); 
     // start location manager 
     LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
     lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist); 
     // Get last location 
     if(!doubleclick){ 
     Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); 
     Log.e("successss", "33333333333333333"); 
     if (loc == null) { 
      display.setText("No GPS location found"); 
     } else { 
      Log.e("successss", "444444444444"); 
      // set Current latitude and longitude 
      currentLon = loc.getLongitude(); 
      currentLat = loc.getLatitude(); 
      Log.e("Location", "currentLat:" + currentLat); 

     } 
     Log.e("successss", "5555555555555"); 
     // Set the last latitude and longitude 
     startStop.setText("Stop"); 
     lastLat = currentLat; 
     lastLon = currentLon; 
     doubleclick = true; 
     } 
     Log.e("successss", "66666666666"); 
     Log.e("Location", "lastLat:" + lastLat); 
     Log.e("Location", "currentLat:" + currentLat); 
     //Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); 
     // Request new location 
     //lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist); 

     // Get new location 
     Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER); 

     // get the current lat and long 
     currentLat = loc2.getLatitude(); 
     currentLon = loc2.getLongitude(); 

     Location locationA = new Location("point A"); 
     locationA.setLatitude(lastLat); 
     locationA.setLongitude(lastLon); 

     Location locationB = new Location("point B"); 
     locationB.setLatitude(currentLat); 
     locationB.setLongitude(currentLon); 
     if (lastLat != 0 || lastLon != 0) { 
      double distanceMeters = locationA.distanceTo(locationB); 
      double distanceKm = distanceMeters/1000f; 

      display.setText(String.format("%.2f Km", distanceKm)); 
      Constant.distance = (String.format("%.2f Km", distanceKm)); 
      start.startAnimation(animRotate); 
     } 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

}; 
} 

請幫助我。謝謝。

+0

你的問題是什麼? – Raptor

+0

@ShivanRaptor,我編輯了我的問題。請幫幫我。這對我很重要。謝謝。對不起,我英文很差。 –

回答

0

我可能是錯了(我還沒有真正挖掘到你的代碼),但會不會是這麼簡單:

這聽起來像你不斷地從一些點x =(0計算距離, 0)。當然,當你回到x時,距離它的距離會減少。

簡單的解決方案:

  • 單獨從總距離
  • 每個「臺階」計算每個你計算距離的時間的距離,重新出發點(移動X到你的當前位置),並將新距離添加到總數。
+0

謝謝兄弟。你能告訴我,我該怎麼做。我試過這個,但有些地方我犯了錯誤。請。 –

+0

它的工作原理。非常感謝你。 –

+0

很高興聽到它! :) – Kjartan

1

你的問題是在這裏:

double distanceMeters = locationA.distanceTo(locationB); 
double distanceKm = distanceMeters/1000f; 
display.setText(String.format("%.2f Km", distanceKm)); 
Constant.distance = (String.format("%.2f Km", distanceKm)); 
start.startAnimation(animRotate); 

Location.distanceTo(Location)方法計算meters的距離。 %.2f只會在逗號後面顯示2位數並剪切最後一位數字。因爲1 meter/1000f = 0.001 km。這就是爲什麼你永遠不會看到TextView中的第三位數字。距離< 10米的結果將顯示爲0.00。

+0

我走了0.14公里,當我回到它像0.13,0.12,0.01 0.01 –

+0

@MohammadRajob下降你的預期結果是什麼? –

+0

當我點擊一個按鈕時,它將開始計算我走路時的距離。輸出最初爲0.00km,然後是0.01km,然後是0.02km .....當我回退時,它會增加,直到我再次點擊按鈕。 –