Redis之GEO存储地理位置信息的使用
作者:空城1995 时间:2023-12-22 14:29:49
目录
常用API
geoadd
geopos
geodist
georadiusbymember
geohash
在外卖软件中的附近的美食店铺、外卖小哥的距离,打车软件附近的车辆,交友软件中附近的小姐姐。我们都可以利用redis的GEO地理位置计算得出。
1、Redis 的 Geo 是在 3.2 版本才有的
2、使用 geohash 保存地理位置的坐标
3、使用有序集合(zset)保存地理位置的集合
常用API
命令 | 描述 |
---|---|
geoadd | 增加某个地理位置的坐标 |
geopos | 获取某个地理位置的坐标 |
geodist | 获取某个地理位置的坐标 |
georadius | 根据给定地理位置坐标获取指定范围内的地理位置集合 |
georadiusbymember | 根据给定地理位置获取指定范围内的地理位置集合 |
geohash | 获取某个地理位置的 geohash 值 |
geoadd
API:geo key longitude latitude member [longitude latitude member ..]
功能:增加地理位置信息
演示:
127.0.0.1:6379> geoadd cities:locations 116.28 39.55 beijing
(integer) 1
127.0.0.1:6379> geoadd cities:locations 116.28 39.55 beijing
(integer) 0
127.0.0.1:6379> geoadd cities:locations 117.12 39.08 tianjin
(integer) 1
127.0.0.1:6379> geoadd cities:locations 114.29 38.02 shijiazhuang 118.01 39.38 tangshan 115.29 38.51 baoding
(integer) 3
geopos
API:geopos key member [member]
功能:增加地理位置信息
演示:
127.0.0.1:6379> geopos cities:locations beijing
1) 1) "116.28000229597091675"
2) "39.5500007245470826"
127.0.0.1:6379> geopos cities:locations beijing tianjin
1) 1) "116.28000229597091675"
2) "39.5500007245470826"
2) 1) "117.12000042200088501"
2) "39.0800000535766543"
geodist
API:geodsit key member member1 member2
功能:获取两个地理位置的距离
#unit:m(米)、km(千米)、mi(英里)、ft(尺)
演示:
127.0.0.1:6379> geodist cities:locations beijing tianjin km
"89.2061"
georadiusbymember
API:georadiusbymember key member raidusm|km|ft|mi [withcoord][withdist][withhash][COUNT count][asc|desc][store key][storedist key]
功能:获取两个地理位置的距离
参数:
withcoord:返回结果中包含经纬度
withdist:返回结果中包含距离中心节点位置
withhash:返回结果中包含geohash
COUNT count:指定返回结果的数量
asc|desc:返回结果按照距离中心节点距离做升序或者倒序排列
store key:将返回结果的地理位置信息保存到制定key
storedist key:将返回结果距离中心节点的距离保存到指定键
演示:
查询距离北京550千米以内的所有城市,并按距离升序排列
127.0.0.1:6379> georadiusbymember cities:locations beijing 550 km withdist asc
1) 1) "beijing"
2) "0.0000"
2) 1) "tianjin"
2) "89.2061"
3) 1) "baoding"
2) "143.8646"
4) 1) "tangshan"
2) "149.7479"
5) 1) "shijiazhuang"
2) "242.3263"
geohash
API:geopos key member [member]
功能:获取某个地方的哈希值
演示:
127.0.0.1:6379> geohash cities:locations beijing
1) "wx48ypbe2q0"
来源:https://blog.csdn.net/fy_java1995/article/details/102915179
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springmvc拦截器登录验证示例
![](https://img.aspxhome.com/file/2023/9/95039_0s.png)
Android中js和原生交互的示例代码
java 学习笔记(入门篇)_多选择结构switch语句
C#对称加密(AES加密)每次生成的结果都不同的实现思路和代码实例
C++判断pe文件实例
Spring Security权限管理实现接口动态权限控制
![](https://img.aspxhome.com/file/2023/5/83445_0s.png)
Android自定义View用切图显示字符串
![](https://img.aspxhome.com/file/2023/3/101863_0s.jpg)
C# 9.0新特性——扩展方法GetEnumerator支持foreach循环
Android自定义UI手势密码改进版源码下载
![](https://img.aspxhome.com/file/2023/3/139493_0s.jpg)
Java中Collection、List、Set、Map之间的关系总结
Android中使用GridView实现仿微信图片上传功能(附源代码)
![](https://img.aspxhome.com/file/2023/9/138749_0s.png)
java shiro实现退出登陆清空缓存
C#通过链表实现队列的方法
Java利用cors实现跨域请求实例
Android 获取随机验证码功能示例
SpringBoot自动装配原理小结
![](https://img.aspxhome.com/file/2023/7/71227_0s.png)
基于springboot的RestTemplate、okhttp和HttpClient对比分析
Java 获取当前系统时间的三种方法
java中fork-join的原理解析
![](https://img.aspxhome.com/file/2023/8/59788_0s.jpg)