Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by magonicolas · Jul 27, 2016 at 10:55 AM · locationgpsmaps

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
         
     }
 
 
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mcoorlim · Apr 10, 2018 at 01:43 PM 0
Share

What units are the output in? $$anonymous$$ilometers?

avatar image jeango · May 29, 2019 at 01:42 PM 0
Share

What is that 6371 multiplier about in final_distance? Edit: Oh never$$anonymous$$d: earth radius

avatar image DaDonik jeango · May 29, 2019 at 01:45 PM 0
Share

The radius of our little blue planet...

avatar image JPoenisch · Mar 02, 2020 at 11:04 AM 0
Share

In your calculations why is the longitude nowhere taken into account?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Leonetienne500 · Jul 03, 2019 at 05:49 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges