$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 center 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
To pick the user location:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
userLat = position.coords.latitude;
userLng = position.coords.longitude;
});
}