【发布时间】:2014-08-05 01:35:55
【问题描述】:
在上班途中,我每 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
}
有什么建议吗?
【问题讨论】:
标签: android gps location distance