R
R
Roman Volkov2016-07-18 22:39:45
Laravel
Roman Volkov, 2016-07-18 22:39:45

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

2 answer(s)
N
Nazar Mokrinsky, 2016-07-18
@nazarpc

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.

V
Vyacheslav Plisko, 2016-07-19
@AmdY

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);

no problem to rewrite it in php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question