I have two GEO locations. How can I calculate the distance between them?

GPS coordinates are geographical coordinates on the WGS84 spheroid. Under this model, Vincenty's formulae gives results of sub-millimeter accuracy. For most applications, that's either overkill or actually deceptive, as the earth's surface is not modelled by WGS84 to that scale.

You can compare the accurracy of various methods of distance computation ~~on this page~~ (*broken link; check the source code instead*). As you can see, the spherical and the differential approximations (the latter uses the Pythagorean theorem with the correct local metric) are inaccurate for a lot of cases.

Of the remaining methods, the first one uses the spheroid's mean radius to convert from geographical to geocentrical latitude, whereas the second one uses the cosine rule to get more accurate results in case of 'small' distances (where the definition of 'small' mainly depends on the difference in latitude).

A seperate script containing only these two methods can be found here, which provides a function called `distance()`

and expecting four arguments: the two latitudes, the difference in longitude (all in radians) and a boolean flag indicating whether the distance is 'small'.

If you use the Google Maps API v3 you can calculate the distance as follows:

Include the **Google Maps** JavaScript file with the **geometry library**:

```
http://maps.google.com/maps/api/js?sensor=true&libraries=geometry
```

The distance can be measured now by using the ** computeDistanceBetween()** method:

```
var from = new google.maps.LatLng(49.004, 8.456);
var to = new google.maps.LatLng(49.321, 8.789);
var dist = google.maps.geometry.spherical.computeDistanceBetween(from, to);
```

This page has JavaScript code for computing the distance of two geographical locations, if that is what you're after.

It depends on what level of accuracy you want.

You could work it out by basic triangle trigonomoetry - ie work out the difference between their longitude, that's one side; then the diff between their latitude, that's the second. Now you can calculate the third side (ie the actual distance between the two) easily enough with basic junior school maths.

However, that method ignores the curvature of the earth's surface, so if you need to take that into account, you'll need to start getting a bit more clever. But you won't need to worry about that unless the distances are quite large or you need an very high degree of accuracy. For most purposes the basic trig method is fine.

The other point, of course is that these methods give you a straight-line measurement. This may be what you want, but you may also want to know the distance to travel - ie on the road. This is completely different, as you'd need to have an accurate map of all the relevant roads. If this is what you need, it might be easier to delegate to Google's maps service (or one of several other similar alternatives).

©2020 All rights reserved.