我有一個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
}
};
}
請幫助我。謝謝。
你的問題是什麼? – Raptor
@ShivanRaptor,我編輯了我的問題。請幫幫我。這對我很重要。謝謝。對不起,我英文很差。 –