Redis之GEO存储地理位置信息
在外賣軟件中的附近的美食店鋪、外賣小哥的距離,打車軟件附近的車輛,交友軟件中附近的小姐姐。我們都可以利用redis的GEO地理位置計(jì)算得出。
-
1、Redis 的 Geo 是在 3.2 版本才有的
-
2、使用 geohash 保存地理位置的坐標(biāo)
-
3、使用有序集合(zset)保存地理位置的集合
常用API
| geoadd | 增加某個(gè)地理位置的坐標(biāo) |
| geopos | 獲取某個(gè)地理位置的坐標(biāo) |
| geodist | 獲取某個(gè)地理位置的坐標(biāo) |
| georadius | 根據(jù)給定地理位置坐標(biāo)獲取指定范圍內(nèi)的地理位置集合 |
| georadiusbymember | 根據(jù)給定地理位置獲取指定范圍內(nèi)的地理位置集合 |
| geohash | 獲取某個(gè)地理位置的 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) 3geopos
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
功能:獲取兩個(gè)地理位置的距離
#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]
功能:獲取兩個(gè)地理位置的距離
參數(shù):
withcoord:返回結(jié)果中包含經(jīng)緯度
withdist:返回結(jié)果中包含距離中心節(jié)點(diǎn)位置
withhash:返回結(jié)果中包含geohash
COUNT count:指定返回結(jié)果的數(shù)量
asc|desc:返回結(jié)果按照距離中心節(jié)點(diǎn)距離做升序或者倒序排列
store key:將返回結(jié)果的地理位置信息保存到制定key
storedist key:將返回結(jié)果距離中心節(jié)點(diǎn)的距離保存到指定鍵
演示:
查詢距離北京550千米以內(nèi)的所有城市,并按距離升序排列
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]
功能:獲取某個(gè)地方的哈希值
演示:
127.0.0.1:6379> geohash cities:locations beijing 1) "wx48ypbe2q0"?
總結(jié)
以上是生活随笔為你收集整理的Redis之GEO存储地理位置信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis之慢查询分析
- 下一篇: Redis的发布订阅模式以及在Sprin