Answer the question
In order to leave comments, you need to log in
How to find points within N-km radius from given coordinates?
There is a table in the database that has lat and lng fields. It is necessary to select all points within a radius of 1 km from the transmitted coordinates.
Are there any php libraries for this?
I only found this https://toster.ru/q/65934, but functions in the muscle are used here.
Answer the question
In order to leave comments, you need to log in
If the radius is 1 km, then the surface distortions will be small. If the accuracy is not critical - take the equator and the length of the earth's surface of the vertical, divide 360 by the numbers obtained, get how many degrees in a kilometer. And then a point ± the required number of degrees, that is, there will be a WHERE with four conditions.
The correct and accurate calculation of coordinates in distance is more complicated, but I suspect that you do not need it.
In that answer there is a ready-made formula
SET earth_radius = 6371.009;
SET axis = (SIN(RADIANS(lat2-lat1)/2) * SIN(RADIANS(lat2-lat1)/2) +
COS(RADIANS(lat1)) * COS(RADIANS(lat2)) *
SIN(RADIANS(long2-long1)/2) * SIN(RADIANS(long2-long1)/2));
SET distance = earth_radius * (2 * ATN2(SQRT(axis), SQRT(1-axis)));
RETURN ROUND(distance, 3);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question