Comments and answers for "How To Get Distance from 2 locations with Unity Location Service"
http://answers.unity.com/questions/1221259/how-to-get-distance-from-2-locations-with-unity-lo.html
The latest comments and answers for the question "How To Get Distance from 2 locations with Unity Location Service"Comment by JPoenisch on JPoenisch's answer
http://answers.unity.com/comments/1704223/view.html
In your calculations why is the longitude nowhere taken into account?Mon, 02 Mar 2020 11:04:16 GMTJPoenischComment by Leonetienne500 on Leonetienne500's answer
http://answers.unity.com/comments/1645530/view.html
It seems to be slightly off.
I have put in known lat long coordinates of two places that are about 400 meters apart and it calculated 5,948,837mWed, 03 Jul 2019 17:49:56 GMTLeonetienne500Comment by DaDonik on DaDonik's comment
http://answers.unity.com/comments/1636069/view.html
The radius of our little blue planet...Wed, 29 May 2019 13:45:39 GMTDaDonikComment by jeango on jeango's answer
http://answers.unity.com/comments/1636067/view.html
What is that 6371 multiplier about in final_distance?
Edit: Oh nevermind: earth radiusWed, 29 May 2019 13:42:06 GMTjeangoComment by mcoorlim on mcoorlim's answer
http://answers.unity.com/comments/1491927/view.html
What units are the output in? $$anonymous$$ilometers?Tue, 10 Apr 2018 13:43:26 GMTmcoorlimAnswer by dandepeched
http://answers.unity.com/answers/1487369/view.html
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;
}Fri, 30 Mar 2018 14:49:46 GMTdandepechedAnswer by Hamu
http://answers.unity.com/answers/1352023/view.html
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);
}Thu, 11 May 2017 14:16:26 GMTHamuAnswer by Bunny83
http://answers.unity.com/answers/1352072/view.html
Well, usually you would use one of [the great-circle distance formulas][1] 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][2].
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.
[1]: https://en.wikipedia.org/wiki/Great-circle_distance
[2]: https://en.wikipedia.org/wiki/Reference_ellipsoid#CoordinatesThu, 11 May 2017 13:04:49 GMTBunny83