- Home /
How To Get Distance from 2 locations with Unity Location Service
How Can I Do it?
I know how to have location with: https://docs.unity3d.com/ScriptReference/LocationService.html , and I have A manually added latitude and longitude, how can I measure the Distance?
Bests!
Answer by Hamu · May 11, 2017 at 02:16 PM
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);
}
What is that 6371 multiplier about in final_distance? Edit: Oh never$$anonymous$$d: earth radius
In your calculations why is the longitude nowhere taken into account?
Answer by Bunny83 · May 11, 2017 at 01:04 PM
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.
Answer by dandepeched · Mar 30, 2018 at 02:49 PM
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;
}
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,837m
Your answer
Follow this Question
Related Questions
Get Real GPS not WiFi GPS 2 Answers
Unity3D - Get smooth speed and acceleration with GPS data 2 Answers
GPS for Unity Android 1 Answer
GPS location string 0 Answers
GPS coordinates (lat/long/altitude) to Unity 3D coordinates (x, y, z) 1 Answer