Development of nearby MySQL users

I have a database with a name userswith latitudeand columns longitude, and I want to work out the closest users (users in the table users) who are within a radius of 50 km to a certain latitude and longitude provided $_POST.

Can you tell me how I can do this? Is there a particular math function to be used, or a function in MySQL, for example, latitude_compare()or something else?

+5
source share
2 answers

You can do something like this:

SELECT *,

(ACOS((SIN(RADIANS(ref_latitude))*SIN(RADIANS(latitude))) + 
(COS(RADIANS(ref_latitude))*COS(RADIANS( latitude ))*COS(RADIANS( longitude)
-RADIANS(ref_longitude)))) * 6371) AS distance

FROM table
ORDER BY distance ASC

Point A: ref_latitude ref_longitude

Point B: Latitude Longitude

My Lat / Lon values ​​are in degrees, for example:

Dresden: 13.721067614881400 / 51.060033646337900

+2

, , . lng .

$q = "SELECT * ( 3959 * acos( cos( radians($current_lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($current_lng) ) + sin( radians($current_lat) ) * sin( radians( lat )))) AS distance FROM users HAVING distance < $radius";
+1

All Articles