当前位置:网站首页>Redis学习笔记—地理信息定位(GEO)
Redis学习笔记—地理信息定位(GEO)
2022-06-23 09:00:00 【爱锅巴】
Redis3.2版本提供了GEO(地理信息定位)功能,支持存储地理位置信息用来实现诸如附近位置、摇一摇这类依赖于地理位置信息的功能,对于需要实现这些功能的开发者来说是一大福音
增加地理位置信息
geoadd key longitude latitude member [longitude latitude member ...]
longitude、latitude、member分别是该地理位置的经度、纬度、成员
添加北京的位置信息
127.0.0.1:6379> geoadd cities 116.28 39.55 beijing
(integer) 1
如果添加成功返回1,已存在会返回0,如果是做修改操作同样使用geoadd,返回0
同时添加三个城市位置信息天津、上海和重庆
127.0.0.1:6379> geoadd cities 117.12 39.08 tianjin 121.47 31.23 shanghai 106.55 29.57 chongqing
(integer) 3
获取地理位置信息
geopos key member [member ...]
获取上海位置信息,同时获取上海和重庆的位置信息
127.0.0.1:6379> geopos cities shanghai
1) 1) "121.47000163793563843"
2) "31.22999903975783553"
127.0.0.1:6379> geopos cities shanghai chongqing
1) 1) "121.47000163793563843"
2) "31.22999903975783553"
2) 1) "106.5499994158744812"
2) "29.5700000136221135"
获取两个地理位置的距离
geodist key member1 member2 [unit]
unit代表单位,不填写默认返回单位是 m(米);可以输入以下四个单位
- m(meters)代表米
- km(kilometers)代表公里
- mi(miles)代表英里
- ft(feet)代表尺
获取上海和重庆的距离
127.0.0.1:6379> geodist cities shanghai chongqing
"1442064.7968"
127.0.0.1:6379> geodist cities shanghai chongqing km
"1442.0648"
获取指定位置范围内的地理信息位置集合
georadius key longitude latitude radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
georadiusbymember key member radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
georadius和georadiusbymember两个命令的作用是一样的,都是以一个地理位置为中心算出指定半径内的其他地理信息位置,不同的是georadius命令的中心位置给出了具体的经纬度,georadiusbymember只需给出成员即可。其中radiusm|km|ft|mi是必需参数,指定了半径(带单位),这两个命令有很多可选参数,如下所示
- withcoord:返回结果中包含经纬度
- withdist:返回结果中包含离中心节点位置的距离
- withhash:返回结果中包含geohash,有关geohash后面介绍
- COUNT count:指定返回结果的数量
- asc|desc:返回结果按照离中心节点的距离做升序或者降序
- store key:将返回结果的地理位置信息保存到指定键
- storedist key:将返回结果离中心节点的距离保存到指定键
获取北京1200km以内的城市
127.0.0.1:6379> georadiusbymember cities beijing 1200 km
1) "beijing"
2) "tianjin"
3) "shanghai"
获取geohash
geohash key member [member ...]
Redis使用geohash将二维经纬度转换为一维字符串,下面操作会返回beijing的geohash值
127.0.0.1:6379> geohash cities beijing
1) "wx48ypbe2q0"
删除地理位置信息
GEO没有提供删除成员的命令,但是因为GEO的底层实现是zset,所以可以借用zrem命令实现对地理位置信息的删除
zrem key member
删除北京的位置信息
127.0.0.1:6379> zrem cities beijing
(integer) 1
边栏推荐
- 3. caller service call - dapr
- 披萨订购设计----简单工厂模式
- 简易学生管理
- Redis学习笔记—Redis与Lua
- 670. Maximum Swap
- 438. Find All Anagrams in a String
- Which is better, semrush or ahrefs? Which is more suitable for GoogleSEO keyword analysis
- Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
- The results of CDN node and source station are inconsistent
- Spirit matrix for leetcode topic analysis
猜你喜欢

通用分页(1)

Custom tag - JSP tag Foundation

Click Add drop-down box
![[cloud native | kubernetes] kubernetes principle and installation (II)](/img/db/dd93bbcac6d0404d44f67d2da12880.png)
[cloud native | kubernetes] kubernetes principle and installation (II)

自定义标签——jsp标签增强

点击添加下拉框

16.系统启动流程

In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain

297. Serialize and Deserialize Binary Tree

'coach, I want to play basketball!'—— AI Learning Series booklet for system students
随机推荐
1、 Software architecture evaluation
Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
Leetcode topic analysis sort colors
Combination sum of leetcode topic analysis
Best time to buy and sell stock II
Best time to buy and sell stock
[learning resources] understand and love mathematics
Combination sum II of leetcode topic analysis
670. Maximum Swap
【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
TDesign update weekly report (the first week of January 2022)
玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
297. Serialize and Deserialize Binary Tree
"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems
[qnx hypervisor 2.2 user manual]5.6.1 silent device during guest shutdown
js 用**遮罩身份证以及手机号的重要数据
[cloud native | kubernetes] kubernetes principle and installation (II)
438. Find All Anagrams in a String
Combination sum III of leetcode topic analysis
Summary of Arthas vmtool command