C,计算两个GPS位置之间的距离是多少? [英] C, Calculating the distance between two GPS locations?
问题描述
如果我有两个GPS位置,说51.507222,-0.1275和48.856667,2.350833,我可以用什么公式来计算两者之间的距离是多少?我已经听说了很多关于haversine公式,但找不到任何关于它的信息,或如何将其应用到C
我已经写了下面的code,然而,这是非常innacurate。任何人都知道为什么吗?我不明白。这个问题来自于函数本身,但我不知道它是什么。
浮动calcDistance(float一,浮动B,浮C,浮动D)
{
浮DLAT;
浮dLon;
DLAT =(C - A);
dLon =(D - B);
DLAT / = 57.29577951;
dLon / = 57.29577951;
浮V_A;
浮V_C;
浮动距离; V_A = SIN(DLAT / 2)*罪(DLAT / 2)+ COS(A)* COS(C)*罪(dLon / 2)*罪(dLon / 2);
V_C = 2 * ATAN2(SQRT(V_A),SQRT(1-V_A));
距离= R * V_C;
返回距离;
}
HTTP: //www.movable-type.co.uk/scripts/latlong.html
这有JavaScript的一个块,我敢肯定,你可以很容易地适应足够温度。
If I have two GPS locations, say 51.507222, -0.1275 and 48.856667, 2.350833, what formula could I use to calculate the distance between the two? I've heard a lot about a haversine formula, but can't find any information about it, or how to apply it to C.
I've written the following code, however, it's very innacurate. Anybody know why? I can't figure it out. The problem comes from the function itself, but I don't know what it is.
float calcDistance(float A, float B, float C, float D)
{
float dLat;
float dLon;
dLat = (C - A);
dLon = (D - B);
dLat /= 57.29577951;
dLon /= 57.29577951;
float v_a;
float v_c;
float distance;
v_a = sin(dLat/2) * sin(dLat/2) + cos(A) * cos(C) * sin(dLon/2) * sin(dLon/2);
v_c = 2 * atan2(sqrt(v_a),sqrt(1-v_a));
distance = r * v_c;
return distance;
}
http://www.movable-type.co.uk/scripts/latlong.html
This has a block of Javascript which I'm sure you could adapt easily enough in C.
这篇关于C,计算两个GPS位置之间的距离是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!