6

Is it ok to compare distances in a classic way (distance between 2 points: d = sqrt(pow(lat2-lat1, 2) + pow(lon2-lon1, 2)) ) using the latitude and longitude returned from google apis without any transformation to meters or sth? I need it just for a comparison to find the closest point from a series of points to a reference point. For example:

Lets say we have two (lat,lon) points: (40.2535425,22.88245345) and (40.2565795,22.8884539) and we want to find witch is closest to (40.2335425,22.83245345). Is it ok to apply the above code to find the distances? Or we need to find the distance, lets say in meters (using the haversine formula or whatever), first for each point from the reference point and then compare the values ?

I ask this question because I don't know what exactly are the values returned by google apis as lat, lon! I mean the are not deg-min-sec are they ?

Thanks...

Darren
  • 68,902
  • 24
  • 138
  • 144
Vassilis
  • 2,878
  • 1
  • 28
  • 43
  • google 'great distance' - you should be able to pass these lat long values to standardized functions to get distances along the surface of the earth. – Randy Apr 04 '11 at 12:23
  • Near the poles, longitude acts funny. At +/-90deg latitude, a degree of longitude == 0 distance. So i imagine the Pythagorean way won't work everywhere. – cHao Apr 04 '11 at 12:28
  • If you are just after the closest don't do the square root in the calculations. If a – Pete Stensønes Apr 04 '11 at 12:33

2 Answers2

3

No, because lines of longitude converge towards the poles. If your points are relatively close together, you can approximate the distance thus:

d = sqrt(pow(lat2-lat1, 2) + cos(lat1)*pow(lon2-lon1, 2))

If you need greater accuracy over large distances, there are several fancy formulae for computing great-circle distances, but I find it simpler to convert to 3D coordinates on a unit circle then do a simple pythagorean distance, followed by 2 sin-1(d/2) to convert back to an angle (though I can understand that some might find not find this simpler, :-).

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • There is a JavaScript formula for calculating the great-circle distance, http://stackoverflow.com/a/27943/368691. – Gajus Apr 30 '13 at 10:20
  • Can "d" be transformed somehow in meters/miles? I am asking because I am confused by the fact that, since there are 180 degrees of latitude but 360 of longitude, I think that "d" can only be useful for comparing distances but not for calculating absolute distances... But I'd be VERY happy if "d" can be somehow transformed :) – ZioBit Oct 26 '15 at 13:54
1

You can also use the computeDistanceBetween() from the new Geometry Library which i think returns the distance in meters

Argiropoulos Stavros
  • 9,436
  • 11
  • 61
  • 79