# 语法
georadius key longitude latitude radius m|km|ft|mi [withcoord][withdist]
[withhash][asc|desc][count count]
# 以给定的经纬度为中心, 找出某一半径内的元素
测试:重新连接redis-cli,增加参数 –raw,可以强制输出中文。不然会乱码
#在china:city 中寻找坐标100 30 半径为1000km的城市
127.0.0.1:6379> georadius china:city 100 30 1000 km
1) "cq"
2) "xa"# withdist 返回位置名称和中心距离
127.0.0.1:6379> georadius china:city 100 30 1000 km withdist
1) 1) "cq"
2) "635.2850"
2) 1) "xa"
2) "963.3171"# withcoord 返回位置名称和经纬度
127.0.0.1:6379> georadius china:city 100 30 1000 km withcoord
1) 1) "cq"
2) 1) "106.54000014066696167"
2) "29.39999880018641676"
2) 1) "xa"
2) 1) "108.92999857664108276"
2) "34.23000121926852302"#withdist withcoord 返回位置名称 距离 和经纬度 count 限定寻找个数
127.0.0.1:6379> georadius china:city 100 30 1000 km withcoord withdist count 1
1) 1) "cq"
2) "635.2850"
3) 1) "106.54000014066696167"
2) "29.39999880018641676"
127.0.0.1:6379> georadius china:city 100 30 1000 km withcoord withdist count 2
1) 1) "cq"
2) "635.2850"
3) 1) "106.54000014066696167"
2) "29.39999880018641676"
2) 1) "xa"
2) "963.3171"
3) 1) "108.92999857664108276"
2) "34.23000121926852302"
georadiusbymember 解析:
# 语法
georadiusbymember key member radius m|km|ft|mi [withcoord][withdist]
[withhash][asc|desc][count count]
# 找出位于指定范围内的元素,中心点是由给定的位置元素决定
测试:
127.0.0.1:6379> georadiusbymember china:city bj 1000 km
1) "bj"
2) "xa"
127.0.0.1:6379> georadiusbymember china:city sh 400 km
1) "hz"
2) "sh"
geohahs解析:
# 语法
geohash key member [member…]
# Redis使用geohash将二维经纬度转换为一维字符串,字符串越长表示位置更精确,两个字符串越相似表示距离越近。
测试:
127.0.0.1:6379> geohash china:city bj cq
1) "wx4sucu47r0"
2) "wm5z22h53v0"
127.0.0.1:6379> geohash china:city bj sh
1) "wx4sucu47r0"
2) "wtw6sk5n300"
zrem
GEO没有提供删除成员的命令,但是因为GEO的底层实现是zset,所以可以借用zrem命令实现对地理位置信息的删除
127.0.0.1:6379> zrange china:city 0 -1
1) "cq"
2) "xa"
3) "sz"
4) "wh"
5) "hz"
6) "sh"
7) "bj"
127.0.0.1:6379> zrem china:city bj
(integer) 1
127.0.0.1:6379> zrem china:city sh
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "cq"
2) "xa"
3) "sz"
4) "wh"
5) "hz"
127.0.0.1:6379>