October 22, 2024
Chicago 12, Melborne City, USA
C#

Calculating the distance between two GPS locations


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;
}



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video