SELECT question

Let's say I have a table containing a list of cities:

city | latitude | longitude
---------------------------
XX   | 34.800   | 48.550

Assuming I have an approximate location (latitude / longitude) of the user, how do I find the nearest city? That is, how do I find a city whose latitude and longitude is closest to the lat / long user?

+3
source share
3 answers

Departure

Creating a storage locator using PHP, MySQL and Google Maps

the calculation method presented there does not depend on Google Maps, you should be able to get the full algorithm from there.

. , , .

+2

Google api ,

:)

0

MySQL

.

3600 * acos (sin (latitude2_rads)           * sin (latitude1_rads) + cos (latitude2_rads)           * cos (latitude1_rads)           * cos (longitude1_rads - longitude2_rads))

, ( -7 , -14 - )

, lat/lon :

select * FROM NDB as c1
order by  acos(sin(radians(-7))
          * sin(radians(latitude)) + cos(radians(-7))
          * cos(radians(latitude))
          * cos(radians(longitude) - radians(-14)))
limit 0,1

, 22 706 , 0.163 .

If performance is a problem, then it might be better to pre-compute the distance from all fixed snap points, and then use this instead of computing in SQL.

0
source

Source: https://habr.com/ru/post/1734771/


All Articles