前言

支持 geo 系列命令的 redis 版本从 3.2.0 起开始才可以使用,所以之前版本就不要想了。

函数列表

geoadd – 将指定的地理空间项(纬度,经度,名称)添加到指定的键, 数据作为有序集存储在 redis 中。

geoadd key longitude latitude member [longitude latitude member …]

key – 存储在 redis 中的指定的键

longitude – 经度

latitude – 纬度

member – 成员名称

<?php
  $redis->geoadd("city", 117.224311, 39.111515, "天津")
// 1
    $redis->geoadd("city", 116.40378, 39.91544, "北京", 121.473913, 31.222965, "上海")
// 2
?>

geopos – 返回由key处的有序集表示的地理空间索引的所有指定成员的位置(经度,纬度)。

geopos key member [member ...]

key – 存储在 redis 中的指定的键

member – 成员名称

<?php
  $redis->geopos("city", "天津") // array ( [0] => array ( [0] => 117.22431153059005737 [1] => 39.11151424175071867 ) )
  $redis->geopos("city", "天津", "北京") // array ( [0] => array ( [0] => 117.22431153059005737 [1] => 39.11151424175071867 ) [1] => array ( [0] => 116.40378087759017944 [1] => 39.91543907825245441 ) )
?>

geodist – 返回由有序集合表示的地理空间索引中的两个成员之间的距离。

geodist key member1 member2 [unit]

key – 存储在 redis 中的指定的键

member – 成员名称

unit – 单位 m(米),km(千米),mi(英里),ft(英尺)

<?php
  $redis->geodist("city","天津", "北京","km") //113.8093
?>

georadius – 使用geoadd返回包含地理空间信息的已排序集合的成员,这些信息位于中心位置和与中心的最大距离(半径)指定区域的边界内。

georadius key longitude latitude radius unit(m|km|ft|mi) [withcoord] [withdist] [withhash][count count] [asc|desc] [store key][storedist key]

key – 存储在 redis 中的指定的键

longitude – 经度

latitude – 纬度

radius – 半径

unit – 单位 m(米),km(千米),mi(英里),ft(英尺)

withcoord 返回目标的经纬度

withdist 返回距离中心点的距离

withhash 返回 52位 无符号整数的 geohash 有序集合分数

count 返回条数
asc|desc 正序排序|倒序排序

<?php
  $redis->georadius("city", 117.224311, 39.111515, 1000, "km", ['withdist','asc'])
    // array ( [0] => array ( [0] => 上海 [1] => 958.4076 ) [1] => array ( [0] => 北京 [1] => 113.8092 ) [2] => array ( [0] => 天津 [1] => 0.0001 ) )
    $redis->georadius("city", 117.224311, 39.111515, 1000, "km", ['withcoord','withdist','asc','count'=>1])
    // array ( [0] => array ( [0] => 天津 [1] => 0.0001 [2] => array ( [0] => 117.22431153059005737 [1] => 39.11151424175071867 ) ) )
?>

georadiusbymember – 这个命令与georadius完全相同,区别在于该命令返回的是成员附近的所有成员

georadiusbymember key member radius unit(m|km|ft|mi) [withcoord] [withdist] [withhash][count count] [asc|desc] [store key][storedist key]

key – 存储在 redis 中的指定的键

member – 成员名称

radius – 半径

unit – 单位 m(米),km(千米),mi(英里),ft(英尺)

withcoord 返回目标的经纬度

withdist 返回距离中心点的距离

withhash 返回 52位 无符号整数的 geohash 有序集合分数

count 返回条数
asc|desc 正序排序|倒序排序

<?php
  $redis->georadiusbymember("city", "天津", 200, "km", ['withcoord', 'withdist', 'asc'])
  //array ( [0] => array ( [0] => 天津 [1] => 0.0000 [2] => array ( [0] => 117.22431153059005737 [1] => 39.11151424175071867 ) ) [1] => array ( [0] => 北京 [1] => 113.8093 [2] => array ( [0] => 116.40378087759017944 [1] => 39.91543907825245441 ) ) )
?>

geohash – 返回有效的geohash字符串

geohash key member [member …]

key – 存储在 redis 中的指定的键

member – 成员名称

<?php
  $redis->geohash("city", "天津", "北京")
  // array ( [0] => wwgqe801h60 [1] => wx4g0f6sk90 )