上班途中,我每隔1秒跟踪一次坐标。当我得到两点之间的距离,并在TextView中显示它时,我得到的数字比我应该高得多。例如,一条9.5英里的路线最终显示为14,000英里的球场。以下是我的代码的副本:
GPS更新处理程序/功能:
public void onGPSUpdate(Location location)
{
//Get Milestone prefs
SharedPreferences settings = getSharedPreferences("AppPrefs", 0);
Integer milestone = settings.getInt("milestone", 5);
//Get current Speed and Distance
speed = location.getSpeed();
Float newDistance;
if(oldLocation == null){
oldLocation = new Location("Point B");
oldLocation = location;
newDistance = 0f;
} else {
newDistance = getDistanceInMiles(location, oldLocation);
}
// Make sure the user is actually moving and that we have an old location
// Add new distance to total
if(roundDecimal(convertSpeed(speed),2) > 0 && oldLocation != null){
distance = distance + newDistance;
}
// Format the distance to 1 decimal
String formattedDistance = String.format("%.1f", distance);
setText(R.id.miles_message, formattedDistance + " ");
}
计算里程数函数:
public float getDistanceInMiles(Location p1, Location p2){
float[] distance1 = new float[1];
Double latFn = p1.getLatitude();
Double lonFn = p1.getLatitude();
Double newLat = p2.getLatitude();
Double newLon = p2.getLatitude();
Location.distanceBetween(latFn,lonFn,newLat,newLon, distance1); //in meters
float distanceInMeters = distance1[0];
float distanceInMiles = distanceInMeters * 0.00062137f;
return distanceInMiles; //in miles
}
有什么建议吗?
答案 0 :(得分:0)
您的代码中存在错误。您将纬度用于每个点的经度和纬度。此外,还有一个内置的距离定位功能。尝试:
public float getDistanceInMiles(Location p1, Location p2){
float distanceInMeters = p1.distanceTo(p2);
float distanceInMiles = distanceInMeters * 0.00062137f;
return distanceInMiles; //in miles
}
答案 1 :(得分:0)
Gabe在我的代码中发现了一个错误。我为每个坐标使用getLatitude()
而不是getLongitude()
和getLatitude()
。我也改变了他的getDistanceInMiles
功能:
public float getDistanceInMiles(Location p1, Location p2){
float distanceInMeters = p1.distanceTo(p2);
float distanceInMiles = distanceInMeters * 0.00062137f;
return distanceInMiles; //in miles
}
我找到的第二个错误是在我设置oldLocation
的第一个条件声明中。 oldLocation
仅在首次运行时设置,导致getdistanceInMiles()
函数每次计算从起始坐标到当前坐标的距离,而不是最后一个坐标与当前坐标之间的距离。
以下是纠正后的代码片段:
public void onGPSUpdate(Location location)
{
//Get Milestone prefs
SharedPreferences settings = getSharedPreferences("AppPrefs", 0);
Integer milestone = settings.getInt("milestone", 5);
//Get current Speed and Distance
speed = location.getSpeed();
Float newDistance;
if(oldLocation == null){
oldLocation = new Location("Point B");
oldLocation = location;
newDistance = 0f;
} else {
newDistance = getDistanceInMiles(location, oldLocation);
oldLocation = location; //Second correction, make sure to set last coordinates to current coordinates
}
// Make sure the user is actually moving and that we have an old location
// Add new distance to total
if(roundDecimal(convertSpeed(speed),2) > 0 && oldLocation != null){
distance = distance + newDistance;
}
// Format the distance to 1 decimal
String formattedDistance = String.format("%.1f", distance);
setText(R.id.miles_message, formattedDistance + " ");
}
//First, simplified distance function to Gabe's suggestion
public float getDistanceInMiles(Location p1, Location p2){
float distanceInMeters = p1.distanceTo(p2);
float distanceInMiles = distanceInMeters * 0.00062137f;
return distanceInMiles; //in miles
}