当前位置:网站首页>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”
边栏推荐
- 二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
- C thread transfer parameters
- PHP method to get the index value of the array item with the largest key value in the array
- 16_Redis_Redis持久化
- Kityformula editor configure font size and spacing
- Base64 编码原来还可以这么理解
- 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
- 05_队列
- Have you learned the wrong usage of foreach
- 牛客练习赛101
猜你喜欢
06_ Stack and queue conversion
Dragonfly low code security tool platform development path
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
Why can't programmers who can only program become excellent developers?
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Kityformula editor configure font size and spacing
Introduction to mathjax (web display of mathematical formulas, vector)
Base64 coding can be understood this way
关于网页中的文本选择以及统计选中文本长度
MFC timer usage
随机推荐
Map介绍
php获取数组中键值最大数组项的索引值的方法
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
Mavn 搭建 Nexus 私服
Kibana basic operation
c语言入门--数组
[noi Simulation Competition] scraping (dynamic planning)
LeetCode_字符串_简单_412.Fizz Buzz
qml 弹窗框架,可定制
传感器数据怎么写入电脑数据库
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
记一次面试
TiDB 软件和硬件环境建议配置
解决el-radio-group 回显后不能编辑问题
【NOI模拟赛】刮痧(动态规划)
AtCoder Beginner Contest 254
Solve the problem that El radio group cannot be edited after echo
871. Minimum refueling times: simple priority queue (heap) greedy question
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
HUSTPC2022