本文介绍了计算行驶距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想开发一个Android应用程序,该应用程序可以计算实际的行进距离并将其显示在文本视图中.这是我使用的代码,但是当您返回起点时,该代码会减少行进距离.任何人都可以帮助修复此代码,使其仅累积距离吗?
I want to develop an android app which can calculate the actual distance traveled and show it in a text view. Here is the code I used but this code decreases the traveled distance when you heading back to the start point. Can anyone help to fix this code so it can accumulate distances only?
display = (TextView) findViewById(R.id.info);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
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();
}
//Set the last latitude and longitude
lastLat = currentLat;
lastLon = currentLon;
}
LocationListener Loclist = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//set Current latitude and longitude
currentLon=loc.getLongitude();
currentLat=loc.getLatitude();
// 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);
double distanceMeters = locationA.distanceTo(locationB);
double distanceKm = distanceMeters / 1000f;
display.setText(String.format("%.2f Km",distanceKm ));
Location locationC = new Location("point c");
locationA.setLatitude(currentLat);
locationA.setLongitude(currentLon);
}
@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
谢谢.
推荐答案
请遵循以下步骤....
Follow these steps....
第1步:
private Location lastLocation;
private double totalDistance = 0;
第2步:在onCreate()
Step 2 : In onCreate()
//Get last location
lastLocation = lm.getLastKnownLocation(lm.GPS_PROVIDER);
第2步:onLocationChanged(位置)//计算距离
Step 2 : onLocationChanged(location) // calculate distance
double distanceMeters = lastLocation.distanceTo(location);
double distanceKm = distanceMeters / 1000f;
// when the location change keep updating the total distance
totalDistance += distanceKm;
lastLocation = location; // assign the current location to lastLocation.
这篇关于计算行驶距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!