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";
Where $lat
and $lng
are the coordinates of your point, and lat/lng are your table columns. The above will list the locations within a 5 nm range. Replace 3959
by 6371
to change to kilometers.
This link could be useful: https://developers.google.com/maps/articles/phpsqlsearch_v3
Edit: I didn’t see you mentioned Java. This example is in PHP but the query is still what you need.