• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Jordan Miller 2 · Dec 28, 2010 at 07:55 PM · androidiphonelocationinfo

calculating distance between two point Iphone

I'm creating an app for the iphone and eventually the Android. I'm trying to calulcate the distance between two points gotten from LocationInfo.latitude and LocationInfo.longitude. does this look right?

latitude1 = LocationInfo.latitude; longitude1 = LocationInfo.longitude;

//much later, when the Latitude and longitude are different values we do this:

latitude2 = LocationInfo.latitude; longitude2 = LocationInfo.longitude;

//then we need the code to figure out how far apart they are:

lat3 = latitude2 - latitude1; long3 =longitude2 - longitude1;

// then is the distnace between them this? (a2 + b2 = c2?)

dist1 = (lat3 *lat3) + (long3*long3); finaldist = Mathf.Sqrt(dist1);

is that the best way to get the distance between two GPS coordinates or is there a better way? does this work for the android and the iphone?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by Ejlersen · Dec 28, 2010 at 08:41 PM

That is certainly one way of doing it, but perhaps not the best. I'm not a sailor, so I know nothing about maps and such. But I've searched a bit around for this and this is the first hit:

http://www.movable-type.co.uk/scripts/latlong.html

And as I thought. The calculations you made gives a straight line from A to B, if it was a flat map. However, the world is not flat (or it was once, so why not again? :-) ). The world is round, so to get the correct distance you need to take this into consideration. The link I gave you should help you with the last piece of the puzzle :)

Comment
Add comment · Show 2 · 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 Jordan Miller 2 · Dec 28, 2010 at 09:17 PM 0
Share

oh thats perfect, thanks for finding that. $$anonymous$$ost of the distances I'll be calculating will be within small distances so the curvature of the earth wont make too much of a difference :) thanks man

avatar image Ejlersen · Dec 28, 2010 at 09:36 PM 0
Share

No problem, dude ;)

avatar image
0

Answer by malisasmaz · Nov 23, 2016 at 12:56 PM

 public void Calc(float lat1, float lon1, float lat2, float lon2)
     {
           
         var R = 6378.137; // Radius of earth in KM
         var dLat = lat2 * Mathf.PI / 180 - lat1 * Mathf.PI / 180;
         var dLon = lon2 * Mathf.PI / 180 - lon1 * Mathf.PI / 180;
         float a = Mathf.Sin(dLat / 2) * Mathf.Sin(dLat / 2) +
         Mathf.Cos(lat1 * Mathf.PI / 180) * Mathf.Cos(lat2 * Mathf.PI / 180) *
         Mathf.Sin(dLon / 2) * Mathf.Sin(dLon / 2);
         var c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1 - a));
         var d = R * c;
         //d= d * 1000f; // meters
         
 
         Text singleText = GameObject.Find("Button").GetComponentInChildren<Text>();
 // write result to the button's text
         singleText.text = d.ToString("F3")+"km";
     }



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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Android / iPhone deployment.. 2 Answers

Unity games and web comunication 1 Answer

TPS Camera with Joystick (Android) 0 Answers

Halo effect perfomance on mobile devices 1 Answer

GUI refresh time on platforms slower than Unity Editor 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges