A
A
Alexey2020-03-03 16:30:18
MySQL
Alexey, 2020-03-03 16:30:18

How to correctly get points with coordinates inside a rectangle?

Hello! At me such situation... There are coordinates of two points of a rectangle (left top and right lower corner). It is necessary to get from the database the coordinates that are inside this rectangle.
The following request came up:

SELECT * FROM `coordinates`
WHERE (`lat` BETWEEN '{$latitude2}' AND '{$latitude1}') AND (`lng` BETWEEN '{$longitude1}' AND '{$longitude2}')

Everything works well, until it turns out that the rectangle intersects the zero meridian, or the equator, because, in fact, the request becomes the opposite:
SELECT * FROM `coordinates`
WHERE (`lat` BETWEEN '-7.987931' AND '6.775799') AND (`lng` BETWEEN '172.152671' AND '-168.106551')

Which is no longer correct. Tell me how to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-03-03
@firedragon

Correct something like this
https://dev.mysql.com/doc/refman/8.0/en/spatial-re...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question