Search code examples
phpmysqlgeolocationgps

Is current location within radius of items stored in a table?


I have a table that stores longitude / latitude / radius ( miles ) per row. What I am trying to figure out is how to select ONLY the rows that my current point is within.

This is the opposite of querying locations within a given radius of my current location, instead, I actually want to query locations that my current point is within.

Any help here would be greatly appreciated.


Solution

  • This is easily a math problem. The distance between 2 points (x1, y1) and (x2, y2) can be calculated as follows:

    statement

    Simple Pythagoras. Query your database, point one is the point you want to find, point two is the one you get from the database, if the result of the above statement is smaller then the radius, it's in your range. (assuming all units match. You'll probably want to convert the lat/lon difference to km/whatever unit your radius is)

    graph