redis特殊数据类型Geo(地理位置)

redis特殊数据类型Geo(地理位置)

常用命令
命令说明
GEOADD添加地理位置的坐标
GEOPOS获取地理位置的坐标
GEODIST计算两个位置之间的距离
GEORADIUS根据用户给定的经纬度坐标来获取指定范围内的地理位置集合
GEORADIUSBYMEMBER根据储存在位置集合里面的某个地点获取指定范围内的地理位置集合
GEOHASH返回一个或多个位置对象的 geohash 值
示例
127.0.0.1:6379> geoadd city 116.397128 39.916527 beijing
(integer) 1
127.0.0.1:6379> geoadd city 108.93425 34.23053 xian
(integer) 1
127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing
(integer) 1
127.0.0.1:6379> geoadd city 113.27324 23.15792 guangzhou
(integer) 1
127.0.0.1:6379> geoadd city 121.48941 31.40527 shanghai
(integer) 1
127.0.0.1:6379> geoadd city 113.88308 22.55329 shenzhen
(integer) 1

127.0.0.1:6379> geopos city beijing
1) 1) "116.39712899923324585"
   2) "39.91652647362980844"
127.0.0.1:6379> geopos city xian nanjing
1) 1) "108.93425256013870239"
   2) "34.23053097599082406"
2) 1) "118.89209836721420288"
   2) "31.32750976275760735"
   
127.0.0.1:6379> geodist city beijing nanjing km
"981.4711"
127.0.0.1:6379> geodist city beijing guangzhou km
"1887.0194"

127.0.0.1:6379> georadius city 100 30 1000 km
1) "xian"
127.0.0.1:6379> georadius city 100 30 2000 km
1) "xian"
2) "shenzhen"
3) "guangzhou"
4) "nanjing"
5) "beijing"
127.0.0.1:6379> georadius city 100 30 2000 km withcoord
1) 1) "xian"
   2) 1) "108.93425256013870239"
      2) "34.23053097599082406"
2) 1) "shenzhen"
   2) 1) "113.88307839632034302"
      2) "22.55329111565713873"
3) 1) "guangzhou"
   2) 1) "113.27324062585830688"
      2) "23.1579209662846921"
4) 1) "nanjing"
   2) 1) "118.89209836721420288"
      2) "31.32750976275760735"
5) 1) "beijing"
   2) 1) "116.39712899923324585"
      2) "39.91652647362980844"
127.0.0.1:6379> georadius city 100 30 2000 km withdist
1) 1) "xian"
   2) "963.6929"
2) 1) "shenzhen"
   2) "1611.4036"
3) 1) "guangzhou"
   2) "1522.2270"
4) 1) "nanjing"
   2) "1811.2832"
5) 1) "beijing"
   2) "1852.5507"
127.0.0.1:6379> georadius city 100 30 2000 km withdist count 2
1) 1) "xian"
   2) "963.6929"
2) 1) "guangzhou"
   2) "1522.2270"
   
127.0.0.1:6379> georadiusbymember city nanjing 1000 km
1) "nanjing"
2) "shanghai"
3) "beijing"
4) "xian"
127.0.0.1:6379> georadiusbymember city nanjing 1000 km withdist count 3
1) 1) "nanjing"
   2) "0.0000"
2) 1) "shanghai"
   2) "246.8158"
3) 1) "beijing"
   2) "981.4711"
   
127.0.0.1:6379> geohash city beijing
1) "wx4g0dtf9e0"
127.0.0.1:6379> geohash city guangzhou shenzhen
1) "ws0e9xg09v0"
2) "ws0br3xnkn0"
127.0.0.1:6379> geohash city guangzhou beijing
1) "ws0e9xg09v0"
2) "wx4g0dtf9e0

## GEO的底层原理是Zset,可以用Zset命令来进行操作
127.0.0.1:6379> zrange city 0 -1
1) "xian"
2) "shenzhen"
3) "guangzhou"
4) "nanjing"
5) "shanghai"
6) "beijing"
127.0.0.1:6379> Zrem city beijing
(integer) 1
127.0.0.1:6379> zrange city 0 -1
1) "xian"
2) "shenzhen"
3) "guangzhou"
4) "nanjing"
5) "shanghai"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值