I'm developing a web app where the user sets a place in a map (Google Maps API) and I store the coordinates in a database. So, at another page, I want to do a search wich consists in getting my local coordinates and retrieve nearby places previously stored at my database based on a radius. How can I do this?
Best How To :
You can use what is called the Haversine formula.
$sql = "SELECT *, ( 3959 * acos( cos( radians(" . $lat . ") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(" . $lng . ") ) + sin( radians(" . $lat . ") ) * sin( radians( lat ) ) ) ) AS distance FROM your_table HAVING distance < 5";
$lng are the coordinates of your point, and
lng are your table columns. The above will list the locations within a 5 nm range. Replace
6371 to change to kilometers.