【发布时间】:2012-02-14 12:06:19
【问题描述】:
我无法跟踪行驶距离。 我在这篇文章中尝试过:Source-Code is completely also there
行驶距离的总和不是我车上显示的实际行驶距离。 2-3公里后,我在车上显示的距离和应用程序有200-800米的差距。在这么短的距离内:o) 我还认识到距离,例如行驶弯道时,正在递减。 我还为监听器尝试了几个 minTime 和 minDistance 值,但没有成功。
我做错了什么? 当我使用“MyTracks”应用程序时,行驶距离计算得非常非常准确。 我只需要一种方法来总结从头到尾的完全驱动方式,而无需在谷歌地图中显示它。真的有这么复杂吗?
亲切的问候 弗兰克
好的,代码中有趣的部分:
public void startListening(final Activity activity)
{
if (locationManager == null)
{
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
}
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
final String bestProvider = locationManager.getBestProvider(criteria, true);
if (bestProvider != null && bestProvider.length() > 0)
{
locationManager.requestLocationUpdates(bestProvider, GPSManager.gpsMinTime,
GPSManager.gpsMinDistance, locationListener);
}
else
{
final List<String> providers = locationManager.getProviders(true);
for (final String provider : providers)
{
locationManager.requestLocationUpdates(provider, GPSManager.gpsMinTime,
GPSManager.gpsMinDistance, locationListener);
}
}
}
和:
private double calcGeoDistance(final double lat1, final double lon1, final double lat2, final double lon2)
{
double distance = 0.0;
try
{
final float[] results = new float[3];
Location.distanceBetween(lat1, lon1, lat2, lon2, results);
distance = results[0];
}
catch (final Exception ex)
{
distance = 0.0;
}
return distance;
}
和:
private void updateMeasurement(){
double distance = calcGeoDistance(startLat,startLon,currentLat,currentLon) * multipliers[unitindex];
String distanceText = "" + RoundDecimal(distance,2) + " " + unitstrings[unitindex];
((TextView)findViewById(R.id.distance)).setText(distanceText);
}
和:
public double RoundDecimal(double value, int decimalPlace)
{
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(decimalPlace, 6);
return bd.doubleValue();
}
哦,在我写下这篇文章时,似乎计算了起点和当前点之间的距离。不是在最后两个测量值之间加上所有值的总和,对吧?
【问题讨论】:
-
请贴出您代码的相关部分。您提供的链接不够。你确定你的 LAT 和 LONG 是正确的吗? distanceBetween 使用大圆路线的近似值进行计算,因此与直线比较会随着距离的增加而引入越来越多的误差(除非您驾驶大圆;))。您的错误约为 10%,因此表明存在编码错误。
-
好的,我自己想出来的。计算是相对于起点进行的。我将其更改为先前的测量值并对值求和。现在好像正在运行。明天早上上班的路上试试:)
标签: android gps tracking distance