当前位置:网站首页>10_Redis_geospatial_命令
10_Redis_geospatial_命令
2022-07-02 12:00:00 【听*雨声】
geospatial(地理位置)
用途:
朋友的定位,附近的人,打车距离计算?
Redis的Geo在Redis3.2版本就推出了!这个功能可以推算地理位置的信息,两地之间的距离,方圆几里的人!
相关命令(只有6个命令)
- GEOADD
- GEODIST
- GEOHASH
- GEOPOS
- GEORADIUS
- GEORADIUSBYMEMBER
geoadd
geoadd // 添加地理位置
规则:两级无法直接添加,我们一般会下载城市数据,直接通读配置文件一次性导入!
127.0.0.1:6379> geoadd china:city 39.90 116.40 beijing
(error) ERR invalid longitude,latitude pair 39.900000,116.400000 // 报错原因是经纬度写反了
参数 key - - - 值(经度、纬度、名称)
有效的经度从-180度到180度。
有效的纬度从-85.05112878度到85.05112878度
127.0.0.1:6379> geoadd china:city 116.40 39.40 beijing
(integer) 1
127.0.0.1:6379> geoadd china:city 121.47 31.23 shanghai
(integer) 1
127.0.0.1:6379> geoadd china:city 106.50 29.53 chongqing 114.05 22.52 shengzhen
(integer) 2
127.0.0.1:6379> geoadd china:city 120.16 30.24 hangzhou 108.96 34.26 xian
(integer) 2
GEOPOS
geopos
获得当前定位:一定是一个坐标值!
geopos // 获取指定城市的经度和纬度
127.0.0.1:6379> geopos china:city beijing
1 ) 1) “116.39999896287918091”
2) “39.40000099577971326”
127.0.0.1:6379> geopos china:city beijing chongqing // 获取北京和重庆的经纬度
1 ) 1) “116.39999896287918091”
2 ) “39.40000099577971326”
2 ) 1 ) “106.49999767541885376”
2 ) “29.52999957900659211”
geodist
geodist
返回两个给定位置之间的距离。
单位:
- m 表示单位为米。
- km 表示单位为千米。
- mi 表示单位为英里。
- ft 表示单位为英尺
127.0.0.1:6379> GEODIST china:city beijing shanghai
“1018116.5434”
127.0.0.1:6379> GEODIST china:city beijing shanghai km // 北京到上海的直线距离:单位km
“1018.1165”
GEORADIUS
georadius
以给定的经纬度为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素
附近的人:首先得获得所有附近的人的地址,定位!
127.0.0.1:6379> GEORADIUS china:city 110 30 1000 km // 查询经度为110,纬度为30,半径1000km范围内的城市
1 ) “chongqing”
2 ) “xian”
3 ) “shengzhen”
4 ) “hangzhou”
GEORADIUSBYMEMBER
GEORADIUSBYMEMBER
找出位于指定元素周围的其他元素!
这个命令和 GEORADIUS 命令一样, 都可以找出位于指定范围内的元素, 但是 的中心点是由给定的位置元素决定的, 而不是像 GEORADIUS 那样, 使用输入的经度和纬度来决定中心点GEORADIUSBYMEMBER
指定成员的位置被用作查询的中心。
127.0.0.1:6379> GEORADIUSBYMEMBER china:city beijing 1000 km // 寻找以北京为中心半径1000km范围内的城市
1 ) “beijing”
2 ) “xian”
GEOHASH
返回一个或多个位置元素的 Geohash 表示。
该命令将返回11个字符的Geohash字符串!
将二维的经纬度转换为一维的字符串,如果两个字符串越接近,那么则距离越近!
127.0.0.1:6379> GEOHASH china:city beijing
1 ) “wx4b0sxcss0”
127.0.0.1:6379> GEOHASH china:city beijing chongqing // 返回北京和重庆的经纬度哈希值
1 ) “wx4b0sxcss0”
2 ) “wm5xzrybty0”
GEO底层的实现原理其实就是Zset !我们可以使用Zset命令来操作geo !
127.0.0.1:6379> zrange china:city 0 -1 // 查看地图中全部的元素
1 ) “chongqing”
2 ) “xian”
3 ) “shengzhen”
4 ) “hangzhou”
5 ) “shanghai”
6 ) “beijing”
127.0.0.1:6379> zrem china:city beijing // 移除指定元素!
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1 ) “chongqing”
2 ) “xian”
3 ) “shengzhen”
4 ) “hangzhou”
5 ) “shanghai”
边栏推荐
- Deploy tidb cluster with tiup
- Mavn builds nexus private server
- GeoServer offline map service construction and layer Publishing
- 【题解】Educational Codeforces Round 82
- 编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
- Set set you don't know
- MFC 控制台打印,弹出对话框
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- 03_线性表_链表
- Arithmetic operations and related exercises in C language
猜你喜欢
随机推荐
XML配置文件
C语言中的算术运算及相关练习题
TiDB 集群最小部署的拓扑架构
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
Practical debugging skills
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
info [email protected] : The platform “win32“ is incompatible with this module.
你不知道的Set集合
06_ Stack and queue conversion
SQL 后计算的利器 SPL
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Topology architecture of the minimum deployment of tidb cluster
06_栈和队列转换
Set set you don't know
Record an error report, solve the experience, rely on repetition
Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
. Net core logging system
传感器数据怎么写入电脑数据库
【题解】Educational Codeforces Round 82
MFC 控制台打印,弹出对话框










