Geolocation and Haversine formula

╄→гoц情女王★ 提交于 2019-12-11 03:19:06

问题


I am trying to create a basic web application that detects the users geolocation, queries a mySQL database, and returns all bus stops within say 5 kilometers.

The GTFS feed including the Longitude and Latitude have been inserted into a mySQL database, and I found a example HTML page that provides the Longitude and Latitude of the browser accessing the web application.

I am seeking some help writing the mySQL query that takes this information and returns the results.


回答1:


Although the great circle formula is precise, you don't need the precision in this case. A minute of latitude is about 1 mile (1.6 km). A minute of longitude is about cos(LAT)*1 mile. I would consider selecting for the box of LAT +/- 3 minutes, and LONG +/- (3/cos(LAT)) minutes. If you really need a circle, not a box, then just pretend it's Euclidean coordinates. The error on this scale is less than the length of the bus.

The only tricky part is that the length of a minute of longitude varies depending on how far from the equator you are.




回答2:


In this link you find:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;



来源:https://stackoverflow.com/questions/3902450/geolocation-and-haversine-formula

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