2013-12-18 26 views
0

我有一個應用程序,它計算每隔100米後的距離。有一個按鈕和一個文本視圖。當我點擊按鈕並開始行走時,距離顯示在文本視圖中。當我第二次點擊按鈕到達目的地後,應用程序停止。但是當我第二次啓動應用程序時,它會在文本視圖中顯示前一個距離,但在那裏應該爲0。安卓在TextView中的GPS距離顯示應用程序第二次運行時的前一個距離

這裏是我的活動:

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; 
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); 
        Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); 

        if (loc == null) { 
         display.setText("No GPS location found"); 
        } else { 
         // set Current latitude and longitude 
         currentLon = loc.getLongitude(); 
         currentLat = loc.getLatitude(); 
         Log.e("Location", "currentLat:" + currentLat); 

        } 
        // Set the last latitude and longitude 
        startStop.setText("Stop"); 
        lastLat = currentLat; 
        lastLon = currentLon; 
        doubleclick = true; 
       } 
      } 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); 
       display.setText("0.0km"); 
      } 

     } 
    }); 
} 

LocationListener Loclist = new LocationListener() { 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     // start location manager 
     LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 

     // Get last location 
     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 = loc.getLatitude(); 
     currentLon = loc.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 

    } 

}; 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    display.setText("0.0km"); 

} 



} 

請幫助我。

回答

0

添加這些行之前start.setOnClickListener(new View.OnClickListener() {});

display.setText("0.0km"); 

編輯:

改變這一行的locationChanged listener.like這樣的:

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

滿級將是這樣的:

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; 
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); 

lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
lm.removeUpdates(Loclist); 
display.setText("0.0km"); 

    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); 
        Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); 

        if (loc == null) { 
         display.setText("No GPS location found"); 
        } else { 
         // set Current latitude and longitude 
         currentLon = loc.getLongitude(); 
         currentLat = loc.getLatitude(); 
         Log.e("Location", "currentLat:" + currentLat); 

        } 
        // Set the last latitude and longitude 
        startStop.setText("Stop"); 
        lastLat = currentLat; 
        lastLon = currentLon; 
        doubleclick = true; 
       } 
      } 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); 
       display.setText("0.0km"); 
      } 

     } 
    }); 
} 

LocationListener Loclist = new LocationListener() { 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     // start location manager 
     LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 

     // Get last location 
     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 = loc.getLatitude(); 
     currentLon = loc.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 

    } 

}; 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    display.setText("0.0km"); 

} 



} 
+0

謝謝兄弟。我可以將文本視圖設置爲0.0km,但是我的問題是當我再次走路時,它會計算與前一距離的距離。假設前一次是3.50km,第二次運行後顯示3.51km,3.52km ......但它應該是0.01km,這樣的0.02km。對不起,我的英語不好。 –

+0

看到我編輯的答案。 – Avijit

+0

因此,這些行沒有用處:位置loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);和位置loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); lm.requestLocationUpdates(lm.GPS_PROVIDER,0,0,Loclist);和LocationManager lm =(LocationManager)getSystemService(LOCATION_SERVICE); –

0

組:

display.setText("0.0km"); 

if(!doubleclick)條件爲好。

您也可以使用該位置對象的onLocationChanged參數爲如下因素:

@Override 
    public void onLocationChanged(Location location) { 


     // get the current lat and long 
     currentLat = location.getLatitude(); 
     currentLon = location.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); 
     } 

    } 

編輯:

LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 

獲取參考位置管理。

Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); 

位置管理器會跟蹤您可以直接使用此方法獲取的上次已知位置。

lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist); 

這就像增加了一個監聽器來改變位置,所以每當位置改變onLocationChange被調用時。 現在,您已經完成了一個按鈕的按鈕已經不需要再次做。您的監聽器已連接並將多次調用onLocationChange。

+0

因此,這些行沒有用處:位置loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);和位置loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); lm.requestLocationUpdates(lm.GPS_PROVIDER,0,0,Loclist);和LocationManager lm =(LocationManager)getSystemService(LOCATION_SERVICE); –

+0

在onLocationChanged中,通過調用getLastKnownLocation獲得與您獲得的相同的新位置。是的,您不需要再次這樣做。 –

+0

這意味着onLocationChanged「location」和Location loc2的參數= lm.getLastKnownLocation(lm.GPS_PROVIDER);是一樣的嗎? –