从2点计算速度
我知道这个问题已经被问了很多次,但还没有让我满意。我正在尝试使用 Android 设备的 GPS 来计算速度。很多人似乎回答说只需使用 Location 对象的 getSpeed() 函数即可。据我了解,getSpeed() 仅适用于 GPS 接收器芯片中内置有速度传感器的某些设备。不管怎样,我希望我的应用程序能够正常工作,因此我使用以下半正矢公式:
private double CalculateHaversineMI(double lat1, double long1, double lat2,double long2) {
double dlong = (long2 - long1) * (Math.PI / 180.0f);
double dlat = (lat2 - lat1) * (Math.PI / 180.0f);
double a = Math.pow(Math.sin(dlat / 2.0), 2)
+ Math.cos(lat1 * (Math.PI / 180.0f))
* Math.cos(lat2 * (Math.PI / 180.0f))
* Math.pow(Math.sin(dlong / 2.0), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = 3956 * c;
return d;
}
现在我想做的是弄清楚如何从中计算速度。有人可以帮我吗?
I know this question has been asked a lot, but not yet to my satisfaction. I am trying to use the GPS of an Android device to calculate speed. A lot of people seem to reply by saying to simply use the getSpeed() function of the Location object. From what I understand though, getSpeed() will only work on certain devices that have a speed sensor built into the GPS receiver chip. I want my application to work regardless of this, so I am using the following haversine formula:
private double CalculateHaversineMI(double lat1, double long1, double lat2,double long2) {
double dlong = (long2 - long1) * (Math.PI / 180.0f);
double dlat = (lat2 - lat1) * (Math.PI / 180.0f);
double a = Math.pow(Math.sin(dlat / 2.0), 2)
+ Math.cos(lat1 * (Math.PI / 180.0f))
* Math.cos(lat2 * (Math.PI / 180.0f))
* Math.pow(Math.sin(dlong / 2.0), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = 3956 * c;
return d;
}
Now what I'm trying to do is figure out how to calculate speed from this. Can anyone help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以看到你的函数返回球体上两点之间的正确路径距离 - 它是
d
。但是,只有当球体上有 2 个彼此不靠近的点时才需要这个公式(意味着它们分开的圆心角不小,1 度的圆心角对应于大约 111 公里的距离,只是为了感觉一下)。如果它们彼此靠近(对于移动的人和低速车辆来说就是这种情况),那么您不需要这个公式。你可以简单且非常精确地用直线近似球体上的弧,然后计算变得微不足道。android.location.Location
中的distanceTo()
函数。What I can see is that your function returns correct path distance between 2 points on a sphere - it is
d
. But, this formula is needed only if you have 2 points on a sphere that are not close to each other (means central angle of their separation is not small, central angle of 1 degree corresponds to distance of 111 km approx, just to get feeling). If they are close to each other (which is the case for people moving and slow speed vehicles), then you do not need this formula. You can simply and very accurately approximate arc on the sphere with the straight line, and then calculation becomes trivial.distanceTo()
function fromandroid.location.Location
.这样就可以计算出距离了。现在您可能还记得,速度 = 距离/时间,因此在沿线的某个位置您需要捕获时间和位置。
另一方面,您使用的公式对于您想要做的事情来说是OTT。基于您所经过的路径远小于地球周长这一事实,您最好进行一些近似。然后你可以得到一个更简单的公式。
That calculates the distance. Now as you may recall, speed = distance / time, so somewhere along the line you need to capture the time as well as the position.
On another note, the formula that you are using is way OTT for what you are trying to do. You would be better off making a number of approximations based on the fact that the path you are traversing is much less than the circumference of the earth. Then you could arrive at a much simpler formula.