Find nearest places by latitude and longitude

谁说我不能喝 提交于 2019-12-05 06:57:26

问题


I have this website that stores latitude and longitude of some places and I want to display the nearest places (lets say, in a radius of 10km) of a specific place.

How do I search on my MySQL table for these places?


回答1:


I answered this kind of question some years ago at Google Answers and again more recently at Uclue.

I should note that the preferred technical convention is to represent longitudes in the West as negative values, but this is sometimes not observed. It won't make much difference unless you are calculating across the meridian opposite to the Prime Meridian (where longitude wraps around), but it is something to be aware of about your database and consistency with other geographic repositories.

Added: As outlined in the Uclue question linked above, the aim is to compute two pairs of values, MINLATITUDE/MAXLATITUDE and MINLONGITUDE/MAXLONGITUDE, so that all points within a given distance of a given location are included in the results of SQL query:

SELECT * FROM Locations  
    WHERE (latitude  between MINLATITUDE  and MAXLATITUDE )  
    and   (longitude between MINLONGITUDE and MAXLONGITUDE)  

Starting with MySQL 5.0 we can bundle the computations of the MIN/MAX pairs together with the SELECT in a convenient stored procedure. We can even run through the results of such a query with a cursor, and check the exact "great circle" distance if such precision is desired. However I'd guess that a simplified approach, dynamically constructing the above query from a calling framework such as Python or C++, would make sense for most applications (in order to minimize work done by the database server).

Let me know if I should elaborate on those possibilities.



来源:https://stackoverflow.com/questions/4626886/find-nearest-places-by-latitude-and-longitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!