问题描述
我开发一个应用程序,它更是你的朋友之间的时移赛车。
I am developing an app which is more of a time-shift racing between your friends.
我需要计算行驶中的车辆的速度,我不想使用 Location.getSpeed()
方法。 (详细解释在底部为什么我不希望使用它)
I need to calculate speed of a moving vehicle, and I don't want to use Location.getSpeed()
method. (Explained in detail in the bottom why I don't want to use it)
我试图计算速度与纬度和经度提供给我的帮助,而这正是我需要帮助。
I am trying to calculate speed with the help of Latitude and Longitude available to me, and this is where I need help.
帮助需要的:我想知道的是:
- 如果该算法是正确
- 我应该计算以厘米为单位,而不是米
- 如果这确实是有任何code /库已经可用。
我使用下面的code:
I am using the following code:
这给了我两个经纬度点之间的距离:
long getDistanceBetweenPoints(double lat1, double lng1, double lat2, double lng2 ){
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
long distanceInMeters = Math.round(6371000 * c);
return distanceInMeters;
}
而下面的code是它是如何被使用的:
if(lastLat == -1 && lastLng == -1){
lastLat = location.getLatitude();
lastLng = location.getLongitude();
lastTimeStamp = location.getTime();
return;
}
long distanceInMeters = getDistanceBetweenPointsAndSetTotal(lastLat, lastLng, location.getLatitude(), location.getLongitude());
long timeDelta = (location.getTime() - lastTimeStamp)/1000;
long speed = 0;
if(timeDelta > 0){
speed = (distanceInMeters/timeDelta);
}
Log.d("Calculations","Distance: "+distanceInMeters+", TimeDelta: "+timeDelta+" seconds"+",speed: "+speed+" Accuracy: "+location.getAccuracy());
lastLat = location.getLatitude();
lastLng = location.getLongitude();
lastTimeStamp = location.getTime();
当我运行它,我得到以下从LogCat中输出:
When I run it, I get following output from that LogCat:
Distance: 0, TimeDelta: 0 seconds,speed: 0 Accuracy: 5.0
详细原因
目标消费者不应该有高品质的GPS芯片高品质的设备,因此总是得到一个非常准确的修复,当设备处于移动状态是不可能的。
Detailed Reasons
The target consumers are not supposed to have high quality devices with high-quality GPS chips, thus always getting a very accurate fix when the device is on the move is not possible.
因此,我不希望依赖于 Location.getSpeed()
方法,因为我已经看到它给出的速度值仅在精度范围内的5〜8米。
I thus don't want to depend on the Location.getSpeed()
method, since I have observed it gives out speed values only when the accuracy is in the range of 5~8 metres.
在正常范围内的精度我得到一般情况下是10-15米,而 getSpeed()
不给任何速度。即使 hasSpeed()
开始返回假的。
The normal accuracy ranges I am getting in general circumstances is 10-15 metres, and getSpeed()
doesn't give any speed. Even hasSpeed()
starts returning false.
我一直在摆弄我的头,围绕这件事超过3天,在这任何帮助将是深深的AP preciated。
I have been tinkering my head around this thing for more than 3 days, any help in this would be deeply appreciated.
许多在此先感谢!
推荐答案
我开发 MyTrails ,一个Android测绘和跟踪应用程序,并像你,我先是用很粗的位置API的谷歌认为可以在Android中,包括努力。
I develop MyTrails, an Android mapping and tracking app, and like you I struggled at first with the very crude location APIs Google has seen fit to include in Android.
hasSpeed()为假时,GPS芯片不具有足够好固定根据dopler效果来计算速度。即使是这样,我平时不信任的速度,如果是小于5公里每小时左右。
hasSpeed() is false when the GPS chip doesn't have a good enough fix to compute speed based on dopler effect. Even when it does, I usually don't trust the speed if it's less than 5km/h or thereabouts.
我的方式处理速度的计算是采用粗低通滤波器:我记录一个指点杆每秒(和最低5米余,根据 LocationManager.requestLocationUpdates()
,并计算出最近的速度,我回去几件样品得到一个具有足够的距离分开(但不超过30s之前),并执行你做了平均。
The way I handle speed calculations is by using a crude low-pass filter: I record a trackpoint every second (and a minimum of 5m apart, based on LocationManager.requestLocationUpdates()
, and to calculate the recent speed, I go back a few samples to get one that is a sufficient distance apart (but no more than 30s prior), and perform the averaging you're doing.
我使用Location.distanceBetween()的实际距离计算。要注意的是它不能在一个非常小的(但不幸的)数量的设备,所以半正矢方法,你有可能是一个更好的选择。你可能想,虽然检查它,我有什么是
I'm using Location.distanceBetween() for the actual distance calculation. Beware that it fails on a very small (but unfortunate) number of devices, so the haversine method you have may be a better bet. You may want to check it though, what I have is
/**
* Gets distance in meters, coordinates in RADIAN
*/
private static double getDistance(double lat1, double lon1, double lat2, double lon2) {
double R = 6371000; // for haversine use R = 6372.8 km instead of 6371 km
double dLat = lat2 - lat1;
double dLon = lon2 - lon1;
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
//double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
// simplify haversine:
//return 2 * R * 1000 * Math.asin(Math.sqrt(a));
}
(注意1000倍)
(note the 1000 factor)
这篇关于计算速度没有getSpeed导航的应用程序()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!