开发者

MySQL Lat/Lon radius search

开发者 https://www.devze.com 2023-03-13 12:39 出处:网络
I have a table with zipcode(int) and Location(point).I\'m looking for a MySql query or function.Here is an example of the date.I\'d like to return 100 miles.

I have a table with zipcode(int) and Location(point). I'm looking for a MySql query or function. Here is an example of the date. I'd like to return 100 miles.

37922|POINT(35.85802 -84.11938)

Is there an easy query to achieve this?

Okay so I have this

select x(Locat开发者_开发问答ion), Y(Location) FROM zipcodes

This will give me my two points, but how do i figure out whats within a distance of x/y?


The query to do this is not too hard, but is slow. You would want to use the Haversine formula.

http://en.wikipedia.org/wiki/Haversine_formula

Converting that to SQL should not be too difficult, but calculating the distance for every record in a table gets costly as the data set increases.

The work can be significantly reduced by using a geohash function to limit the locus of candidate records. If accuracy is important, the Haversine formula can be applied to the records inside a geohash region.

If the mysql people never completed their GIS and Spatial extension, consider using ElasticSearch or MongoDB.

There is a pretty complete discussion here: Formulas to Calculate Geo Proximity

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号