What will be the best way (or) Is there a Google's way already to Calculate the simple Straight Line
Distance between Two Points, based on Lat/Lng or even on Postal/Zip Code is possible?

- 17,821
- 44
- 135
- 217
-
what language are you using? Or are you trying to do this with Google Maps? – geocodezip Jan 16 '13 at 18:38
4 Answers
I found the answer by myself, from somewhere else.
Yes, there is a native solution from Google already, at: https://developers.google.com/maps/documentation/javascript/reference?hl=en-US#spherical
All I need to do is to call the method: 'google.maps.geometry.spherical.computeDistanceBetween (latLngA, latLngB);'
(Ofcourse I also need to include the additional/required '.js')

- 17,821
- 44
- 135
- 217
-
From the google name spacing I'd assume this is spherical distance and not "straight" as you stated in your question. (I'm looking for the same thing, but I want a cheap calculation) – Aidan Ewen Jan 20 '16 at 13:38
"Best" is a pretty vague criterion. If you're able to assume the earth is a perfect sphere, then you want the simple formula for great circle distance. See for example the Wikipedia article. With this assumption your distance can be off by something less than half a percent.
The actual shape of the earth is actually a slightly oblate spheroid. The surface distance on this shape is more complicated to compute. See Ed Williams' work in javascript. Maybe he will let you use his code. If not he gives relevant references.

- 46,253
- 4
- 58
- 96
A free solution is at http://ezcmd.com/apps/app_ezip_locator#ezip_locator_api
Can help you find distance between two lat,long coordinates in miles or Km.
Or, you could try http://ezcmd.com/apps/app_geo_postal_codes#geo_postal_codes_api
The "best" way depends on several things. Can you provide a little more background as to how accurate and/or what's the desired application? The google.maps.DirectionsService class will allow you to calculate the driving distance client side with javascript, but if you want an accurate straight line distance you could use postgresql + postgis server side. Calculating accurate distances with lat/lng can get tricky with the different projections of the earth depending on the range of points and distances involved.

- 44
- 1
- 8
-
Sorry, i mean just simple `straight` Distance, whatever the geography is there between. – 夏期劇場 Jan 16 '13 at 17:56
-
calculating straight distances with lat/lng coordinates uses some fairly complex formulas depending on the distances involved, but one way to get a "simple" all around the world accurate straight line distance would be to do a distance query in postgresql + postgis – scrivy Jan 16 '13 at 18:04
-
How important is accuracy? the world just isn't as round as you'd like it to be :-P – scrivy Jan 16 '13 at 18:05
-
-
unless the point distance being calculated is projected onto a 2d plane (to make the math simpler) straight generally implies taking the curvature of the earth into account – scrivy Jan 16 '13 at 23:31
-
lat/long distances are again......complicated. what are you trying to do? there are several formulas to use depending on short vs long calculations, or the easiest solution, postgis. – scrivy Jan 17 '13 at 01:28
-
postgis isn't easy either.... but it contains the "magic" accurate distance function you're probably looking for. there are different ways to implement it in javascript but they aren't very accurate. check out http://www.movable-type.co.uk/scripts/latlong.html – scrivy Jan 17 '13 at 01:30
-
Sorry all here but I just found the pure simple solution (answer) by myself, which is supported by Google itself already. – 夏期劇場 Jan 17 '13 at 05:31
-