Answer the question
In order to leave comments, you need to log in
Yandex Maps - how to display objects within a radius of n from MySQL?
Given:
the model "Address" with the coordinates of the width and longitude
is stored in Mysql decimal(9,6)
) It is
necessary
to select from the database by radius and by rectangle at a given distance
If the rectangle is clear how to do it, but what about the radius?
Answer the question
In order to leave comments, you need to log in
Everything is very simple. Given that the coordinates do not take into account the curvature of the earth, knowing the approximate radius of the planet, you can calculate the distance from one object to another using an easy formula.
Well, in SQL it is expressed something like this:
SELECT
`id`,
(
6371 *
acos(
cos( radians( %lat ) ) *
cos( radians( `lat` ) ) *
cos(
radians( `long` ) - radians( %lon )
) +
sin(radians( %lat )) *
sin(radians(`lat`))
)
) as `distance`
FROM
`location`
HAVING
`distance` < %distance
ORDER BY
`distance`
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question