- Home /

# Length between GPS-locations

Hey all Unity developers and programmers :) i am developing a game where i need the length between two GPS-locations in KM. lets say we have two locations points like this: (87.090329, 25.883993) (89.393944, 31.92494) how can i get the length between these two positions? anyone know? if you could code it for me in c#

Best Regards :)

**Answer** by tanoshimi
·
Jan 03, 2015 at 02:56 PM

The most common solution is to approximate using the Haversine formula.

Pseudo-code:

```
var a = sin²(Δφ/2) + cos φ1 * cos φ2 * sin²(Δλ/2);
var c = 2 * atan2( √a, √(1−a) );
var d = R * c;
```

where: φ is latitude, λ is longitude, R is radius of the Earth (use 6,371km if unsure)

Hey thanks for the answer :) but there is an error it says unexpected symbol :

**Answer** by N1warhead
·
Jan 03, 2015 at 04:18 PM

The easiest way I would think is get a map of a place you need..

Such the entire USA.

Okay, find the points of GPS you would like. Then find a line of sight distance, then divide the distance or something like that.

That way you can calculate the distance per grid.

Perhaps you'd be so kind as to expand your answer with an example...? Say, how would you use this approach to calculate the distance between London (51.5,-0.12) and Paris (48.85,2.33)?

Go to google Earth. find the GPS Numbers for each given location...

$$anonymous$$ake a custom grid.

Lets say 10 x 10 grid squares. Okay.

Well, it's 282.3 miles to the locations. gives you 28.3 miles per grid square.

I would use a more advanced grid that could cover literally every single possible GPS location. Such as a 4096 x 4096 pixel image. As that would give you a total of 16,777,216 pixels.

That way you can find the most $$anonymous$$ute distance to calculate given the formula I used above.

It's not hard, all you have to do is calculate the distance per each pixel geographically, which depends on the size of your grid. ideally you would probably want it in the range of feet per pixel not miles. That way it is more accurate.

It's hard to explain, I had to use this said formula to make an advanced neural network to calculate weather patterns and distances to places.

It's not easy to explain, I knew for awhile what I wanted to do, just didn't really understand how, until I did the above, which it may not be directly that hard, but it is. At least to $$anonymous$$ch the computer the distances and stuff it is.

The problem I would have with this is that no 2D representation of the earth (a map) is actually uniformly scaled at every point, for example google maps uses the Gall stereographic model, so two squares of the same size, one at the equator and one at for example 66 degrees latitudinally, the square at the equator will actually contain more 'area' than the other, as the length of the equator is longer than the length of the arctic circle (at 66'33) so the scale that the land is drawn at is smaller.

I think it could be a good approximation over short distances, but I don't think you should use it for world map type distances.

Yeah that is true, I wasn't trying to go for a world wide map when I made my program, it was only for the state I live in... Which is a much smaller area to do things with lol.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.