textview中的android gps距离在应用程序第二次运行时显示上一个距离
我有一个应用程序,每100米计算一次距离。有一个按钮和一个文本视图。当我单击按钮并开始行走时,距离显示在文本视图中。当我第二次单击按钮到达目的地后,应用程序停止。但是当我第二次启动应用程序时,它会显示在文本视图中,前面的距离应该是0 以下是我的活动:textview中的android gps距离在应用程序第二次运行时显示上一个距离,android,gps,Android,Gps,我有一个应用程序,每100米计算一次距离。有一个按钮和一个文本视图。当我单击按钮并开始行走时,距离显示在文本视图中。当我第二次单击按钮到达目的地后,应用程序停止。但是当我第二次启动应用程序时,它会显示在文本视图中,前面的距离应该是0 以下是我的活动: public class Gps extends Activity { TextView display; Button start; boolean doubleclick = false; AnimationDrawable AppNameA
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");
}
}
请帮助我。在开始之前添加这些行。setOnClickListenernew View.OnClickListener{} 编辑: 在locationChanged listener中更改此行。如下所示:
// 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");
}
}
整个课程将是这样的:
// 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");
}
}
设置:
如果!双击条件
您还可以在onLocationChanged参数中使用location对象,如下所示:
@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);
}
}
编辑:
获取对位置管理器的引用
Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
Location manager跟踪您可以使用此方法直接获取的最后一个已知位置
lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist);
这类似于将侦听器添加到位置更改,因此每次位置更改时,都会调用LocationChange。
现在,您只需单击一个按钮就可以完成此操作,无需再次执行此操作。您的听众已连接,将多次呼叫onLocationChange。谢谢兄弟。我可以将文字视图设置为0.0km,但我的问题是,当我再次行走时,它会计算出与前一距离的距离。假设前一次是3.50公里,第二次运行后显示为3.51公里,3.52公里……但应该是0.01公里,0.02公里,像这样。对不起,我的英语不好。因此,这些行没有任何用处:Location loc2=lm.getlastnownlocationlm.GPS\u PROVIDER;和Location loc=lm.getlastnownlocationlm.GPS\u提供者;lm.requestLocationUpdateslm.GPS_提供程序,0,0,位置列表;和LocationManager lm=LocationManager getSystemServiceLOCATION\u服务;你能告诉我为什么我使用Location loc2=lm.getlastnownlocationlm.GPS\u PROVIDER;这条线。我可以使用currentLat=loc2.getLatitude;currentLon=loc2.getLongitude;对于当前lat和lon,这两条线是什么:lastLat=当前lat;lastLon=currentLon;?拆下这些线。正如您已经在onclick listener中所做的那样,这些行没有任何用处:Location loc2=lm.getlastnownlocationlm.GPS\u PROVIDER;和Location loc=lm.getlastnownlocationlm.GPS\u提供者;lm.requestLocationUpdateslm.GPS_提供程序,0,0,位置列表;和LocationManager lm=LocationManager getSystemServiceLOCATION\u服务;在onLocationChanged内部,您可以通过调用getLastKnownLocation获得与您获得的相同的新位置,因此您不需要再次执行此操作。这意味着onLocationChanged location和location loc2的参数=lm.getLastKnownLocationlm.GPS\u;是相同的吗?在onlocationchanged中,两个位置对象的值都相同。您能告诉我onlocationchanged中的前四行是指我的活动吗?对不起,这个问题问得不好。
lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist);