I have two GEO locations. How can I calculate the distance between them?
-
1http://stackoverflow.com/questions/1420045/how-to-find-distance-from-the-latitude-and-longitude-of-two-locations – ArK Oct 01 '10 at 11:31
4 Answers
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);

- 40,542
- 28
- 115
- 170
-
Nice but don't forget it's forget until 25 000 request per day after you have to pay so be careful when you use this on your app or website. – Matohawk Jul 08 '14 at 01:36
-
@Matohawk to calculate the distance no query to the G backend is sent! So there is no restrictions to use this function more than 25000 times a day. – powtac Jul 08 '14 at 16:20
-
1Thanks, I don't know that it makes sense so we don't need the google api for that. I believe that google calculate on theirs server the value to optimize the calculation. – Matohawk Jul 09 '14 at 21:22
-
1https://developers.google.com/maps/documentation/javascript/distancematrix @Matohawk is right. Please check this link for complete details. 2,500 free elements per day, calculated as the sum of client-side and server-side queries; enable billing to access higher daily quotas, billed at $0.50 USD / 1000 additional elements, up to 100,000 elements daily. – Yash Aug 07 '17 at 06:49
-
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'.

- 164,997
- 36
- 182
- 240
-
-
@powtac: added example code to http://mercurial.intuxication.org/hg/js-hacks/raw-file/tip/geo/distance.js ; feel free to modify the script to include lat/long conversions if you find that more convenient... – Christoph Mar 17 '11 at 22:00
-
-
@JudahHimango: the scripts can be recovered from https://pikacode.com/mercurial.intuxication.org/js-hacks.tar.gz ; if I don't forget about it, I'll stash them somewhere more accessible soon-ish... – Christoph May 09 '14 at 22:47
-
Hi , the link you mentined above was not working . Can you tell me some other alternate guides – Amaranadh Meda Apr 23 '15 at 15:12
-
@AmarnadhMeda: the link I posted in the comments still works for me; I also added links to the source code to the answer itself – Christoph Apr 23 '15 at 15:37
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).

- 166,037
- 39
- 233
- 307