How to find the nearest geohash in mysql?

2019-09-05 00:46发布

Recently I'm using GeoHash to hash the paired geo-coordinates into a hash value and store it in MySQL. Now I want to find the nearest hash given the other hash. I noticed that MYSQL provide BTree structure to find a range of nearest hashes by using the command "like" in SQL query.

The problem is, how could I find the nearest one instead of giving a range since sometimes I don't know the range.

Could someone give me a hint?

Really appreciate it.

1条回答
2楼-- · 2019-09-05 01:02

QuadTile (or GeoHash?) reversibly turns latitude + longitude into a single number. But to use it to "find nearest" gets complex and messy. Read about Z-ordering. As I understand it, you would need to run 4 queries with ORDER BY and LIMIT; UNION the results together; then check to see which are best.

But it gets tricky to know what to do if the attempt did not find enough items.

SPATIAL indexes provide a more straightforward way.

Here is a technique for efficiently "finding nearest" for large datasets.

查看更多
登录 后发表回答