Posted By

zingo on 12/01/09


Tagged

mysql sql math longitude distance latitude postcode


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

kfair68b
MatthewOnline
g8rpal
Tyster


Calculating distance in Kilometres and Miles from Latitude and Longitude


 / Published in: SQL
 

URL: http://www.sixfive.co.uk/index.cfm/2007/4/9/Calculating-distance-in-Kilometres-and-Miles-from-Latitude-and-Longitude

Using SQL you can have your database return the distance between 2 sets of Lat and Long points, and using some math functions also take into account the curvature of the earth.

  1. SELECT postcode, (3959*acos((sin(#latitude#/57.3) * sin(latitude/57.3))+ (cos(#latitude#/57.3) * cos(latitude/57.3) * cos((#longitude# - longitude)/57.3))))*8/5 AS exactDistance
  2.  
  3. FROM postcode
  4.  
  5. WHERE 1=1
  6.  
  7. ORDER BY (3959*acos((sin(#latitude#/57.3) * sin(latitude/57.3))+ (cos(#latitude#/57.3) * cos(latitude/57.3) * cos((#longitude# - longitude)/57.3))))*8/5

Report this snippet  

You need to login to post a comment.