Getting the Closest Value of Latitude and Longitude in MySQL

Posted by badjography | Posted in How to, Scriplets | Posted on 26-03-2012

0

I just learned today on how to get/compute the closest value of latitude andĀ longitudeĀ using MySQL (see I'm not a mathematician, with this complex computation for me... it makes me have a severe nosebleeds :| ).

If you have a list of coordinates in your database and you are planning to search a coordinates that would return the closest value, this query is right for you. :)

$get_coordinates = 'SELECT lat, lng, SQRT( POW( 69.1 * ( lat - '.$lat.') , 2 ) +
POW( 69.1 * ( '.$lng.' - lng ) * COS( lat / 57.3 ) , 2 ) ) AS distance FROM
table_name ORDER BY distance ASC limit 1';

Reference: MySQL Forum

Write a comment

Security Code: