Wednesday, 6 July 2016

Get Radius in Miles using Latitude & Longitude in MySQL

Here is the trick to find the radius distance in miles directly from the latitude & longitude data store in your mysql table. This is a very helpful trick which i use while creating webservices for mobile apps.


SELECT
  id, (
    3959 * acos (
      cos ( radians(78.3232) )
      * cos( radians( lat ) )
      * cos( radians( lng ) - radians(65.3234) )
      + sin ( radians(78.3232) )
      * sin( radians( lat ) )
    )
  ) AS distance
FROM markers
HAVING distance < 30
ORDER BY distance
LIMIT 0 , 20;

No comments:

Post a comment