26/05
2017
Wallogit.com
2017 © Pedro Peláez
xianyunyehe/geohash
这年头和LBS相关的应用越来越火. 从foursquare的热闹程度就可见一般, 更不用说微信、陌陌了 (什么, 没听过 foursquare... 哥们, 你 out 了). 和 LBS有关的应用一般都包括一些共同的操作, 最常见的一个, 就是找附近的东东(餐馆, 商店, 妞....). 所以, 这里就抛出了一个问题, 怎样才能在大量经纬度数据中检索出附近的点呢?, (*1)
geohash能做到 @一个开发者, (*2)
, (*5)
require_once('geohash.class.php'); $geohash = new Geohash; //得到这点的hash值 $hash = $geohash->encode(39.98123848, 116.30683690); //取前缀,前缀约长范围越小 $prefix = substr($hash, 0, 6); //取出相邻八个区域 $neighbors = $geohash->neighbors($prefix); array_push($neighbors, $prefix); print_r($neighbors);
//得到9个geohash值 Array ( [top] => wx4eqx [bottom] => wx4eqt [right] => wx4eqy [left] => wx4eqq [topleft] => wx4eqr [topright] => wx4eqz [bottomright] => wx4eqv [bottomleft] => wx4eqm [0] => wx4eqw )
, (*6)
SELECT * FROM xy WHERE geohash LIKE 'wx4eqw%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqx%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqt%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqy%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqq%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqr%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqz%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqv%'; SELECT * FROM xy WHERE geohash LIKE 'wx4eqm%';
索引:, (*7)
, (*8)
数据:, (*9)
, (*10)
EXPLAIN SELECT * FROM xy WHERE geohash LIKE 'wx4eqw%';
, (*11)
其他资料: - geohash演示: http://openlocation.org/geohash/geohash-js/ - wiki: http://en.wikipedia.org/wiki/Geohash - 原理: https://github.com/CloudSide/geohash/wiki, (*12)