我有一个计算行驶公里数的应用程序。但是当我在前 1 或 2 公里内开车时。一切都很好,之后我的应用程序变慢了。例如,当我行驶 2 公里时。我的应用程序是 1.93 或当我旅行 70 公里时。我的应用程序是 48 公里。
我还有一个小问题。当我第一次启动应用程序时,我得到很大的值(例如 5536 公里。)当我重新启动程序时,我得到正常值 0.0 公里。这只是我第一次启动 GPS 时如果我关闭 GPS 之后打开我会再次遇到同样的问题。
对不起,我的英语不好。
提前致谢。
public class Gps extends Activity {
TextView display;
double km;
double currentLon=0 ;
double currentLat=0 ;
double lastLon = 0;
double lastLat = 0;
double distance;
double distanceMeters;
double distanceKm;
double distanceKm1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
display = (TextView) findViewById(R.id.info);
LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
display.setText("GPS not found");
if(loc==null){
display.setText("GPS not found");
}
else{
//Get the last latitude and longitude
lastLon=loc.getLongitude();
lastLat=loc.getLatitude();
}
}
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 = lm.getLastKnownLocation(lm.GPS_PROVIDER);
//Request new location
lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
//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);
distanceMeters = locationA.distanceTo(locationB);
distanceKm = distanceMeters / 1000f;
display.setText(String.format("traveled km.\n%.2f km.",distanceKm));
}
@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
}
};
}