Calculating distance in Kilometres and Miles from Latitude and Longitude


/ Published in: SQL
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.