How To Get Distance from 2 locations with Unity Location Service

How Can I Do it?

I know how to have location with: Unity - Scripting API: LocationService , and I have A manually added latitude and longitude, how can I measure the Distance?

Bests!

I know this is pretty late, but to anyone else who is stuck on such an issue, here’s the answer.

 float DegToRad(float deg)
    {
        float temp;
        temp = (deg * PI) / 180.0f;
        temp = Mathf.Tan(temp);
        return temp;
    }

 float Distance_x(float lon_a, float lon_b, float lat_a, float lat_b)
    {
        float temp;
        float c;
        temp = (lat_b - lat_a);
        c = Mathf.Abs(temp * Mathf.Cos((lat_a + lat_b)) / 2);
        return c;
    }

    private float Distance_y(float lat_a, float lat_b)
    {
        float c;
        c = (lat_b - lat_a);
        return c;
    }

    float Final_distance(float x, float y)
    {
        float c;
        c = Mathf.Abs(Mathf.Sqrt(Mathf.Pow(x, 2f) + Mathf.Pow(y, 2f))) * 6371;
        return c;
    }

    //*******************************
//This is the function to call to calculate the distance between two points

    public void Calculate_Distance(float long_a, float lat_a,float long,_b, float lat_b )
    {
        float a_long_r, a_lat_r, p_long_r, p_lat_r, dist_x, dist_y, total_dist;
        a_long_r =DegToRad(long_a);
        a_lat_r = DegToRad(lat_a);
        p_long_r = DegToRad(long_b);
        p_lat_r = DegToRad(lat_b);
        dist_x = Distance_x(a_long_r, p_long_r, a_lat_r, p_lat_r);
        dist_y = Distance_y(a_lat_r, p_lat_r);
        total_dist = Final_distance(dist_x, dist_y);
    //prints the distance on the console
        print(total_dist);
        
    }

Well, usually you would use one of the great-circle distance formulas to basically get the arc between your two coordinates. Since we work with radians you simply need to multiply the resulting angle with the earth radius.

Though keep in mind if you only deal with relatively short distances, using the mean radius would be enough. However if you want to make it more correct you want to calculate the earth radius at the given point using the reference ellipsoid.

Note that we just assume a straight path between the two points along the surface of the ellipsoid. You might want to use the average height of the two points involved as radius.

This code should work:

private float CalculateDistance(float lat_1, float lat_2, float long_1, float long_2)
{
    int R = 6371;

    var lat_rad_1 = Mathf.Deg2Rad * lat_1;
    var lat_rad_2 = Mathf.Deg2Rad * lat_2;
    var d_lat_rad = Mathf.Deg2Rad * (lat_2 - lat_1);
    var d_long_rad = Mathf.Deg2Rad * (long_2 - long_1);

    var a = Mathf.Pow(Mathf.Sin(d_lat_rad / 2), 2) + (Mathf.Pow(Mathf.Sin(d_long_rad / 2), 2) * Mathf.Cos(lat_rad_1) * Mathf.Cos(lat_rad_2));
    var c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1 - a));
    var total_dist = R * c * 1000; // convert to meters

    return total_dist;
}

I just simply calculate by get
last position of vector2 (Latitude ,longitude) and
Latest position vector2(Latitude ,longitude) to Vector2.Distance
it will gain a distance of 2 point, that is.