Skip to content

a349944418/geohash

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

geohash

这年头和LBS相关的应用越来越火. 从foursquare的热闹程度就可见一般, 更不用说微信、陌陌了 (什么, 没听过 foursquare... 哥们, 你 out 了). 和 LBS有关的应用一般都包括一些共同的操作, 最常见的一个, 就是找附近的东东(餐馆, 商店, 妞....). 所以, 这里就抛出了一个问题, 怎样才能在大量经纬度数据中检索出附近的点呢?

geohash能做到 @一个开发者

paypaldonate

Requirements

  • PHP >= 4

Usage

  • 例如: 用iPhone/android手机定位得到理想国际大厦的经纬度: 39.98123848, 116.30683690 然后查找附近的妞
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值
//得到9个geohash值

Array
(
    [top] => wx4eqx
    [bottom] => wx4eqt
    [right] => wx4eqy
    [left] => wx4eqq
    [topleft] => wx4eqr
    [topright] => wx4eqz
    [bottomright] => wx4eqv
    [bottomleft] => wx4eqm
    [0] => wx4eqw
)
  • 范围如图:

  • 用sql语句查询
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%';
  • 看一下是否用上索引 (一共有50多万行测试数据):

索引:

数据:

EXPLAIN SELECT * FROM xy WHERE geohash LIKE 'wx4eqw%';

其他资料:

About

Geohash generation class for php

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%