2
$\begingroup$

I'm using a query I found on stackoverflow for finding locations near a given latitude/longitude in a database. Below is the query. Entirely out of curiosity, can someone explain what exactly it's doing (the math bits, not the sql)? 44.xxx and -93.xxx is the lat/long to start from.

Thanks!

SELECT id, ( 3959 *  acos( cos( radians(44.96577) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-93.268433) ) + sin( radians(44.907411) ) * sin( radians( latitude ) ) ) )  AS distance  FROM ivr_sign.property  HAVING distance < 10  ORDER BY distance ASC, id ASC LIMIT 10 

1 Answers 1

3

This formula uses the Spherical law of cosines to calculate the great circle distance between two points, given their latitudes and logitudes.

In particular, it calculates the distance between fixed point (lat $44.\cdots$, long $-93.\cdots$) with the known points in the database.

The 3959 is (an approximation of) the radius of the earth in miles.