M
M
Maxim Fedorov2017-09-30 23:25:49
PHP
Maxim Fedorov, 2017-09-30 23:25:49

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

1 answer(s)
A
Andrzej Wielski, 2017-09-30
@Maksclub

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`

Where %lat and %lon are coordinates, %distance is the radius in kilometers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question