Calculate distance between two gps location using mysql

By: Ryan Wong at

When you need to calculate the distance between 2 gps cordinates(latitude, longitude) you can use this mysql query.

1
2
3
4
5
'SELECT (((acos(sin((' + latitude +
'*pi()/180)) * sin((`latitude`*pi()/180))+cos((' + latitude +
'*pi()/180)) * cos((`latitude`*pi()/180))' +
'* cos(((' + longitude + '- `longitude`)* pi()/180))))*180/pi())*60*1.1515* 1.609344) as distance ' +
'FROM table WHERE 1 ORDER BY distance ASC

You can add a having statement or distance conditional to filter out rows.
Hope this helps.