Use of GEO of Redis to store geographic location information

  • 2021-11-29 23:52:16
  • OfStack

Directory Common API geoaddgeoposgeodistgeoradiusbymembergeohash

In the nearby gourmet shops in the take-out software, the distance between the take-out brothers, the vehicles near the taxi software, and the little sisters near the dating software. We can all calculate it by using GEO geographical location of redis.

1. The Geo of Redis was only available in version 3.2

2. Use geohash to save the coordinates of the geographic location

3. Use ordered sets (zset) to save the set of geographical locations

Commonly used API

命令 描述
geoadd 增加某个地理位置的坐标
geopos 获取某个地理位置的坐标
geodist 获取某个地理位置的坐标
georadius 根据给定地理位置坐标获取指定范围内的地理位置集合
georadiusbymember 根据给定地理位置获取指定范围内的地理位置集合
geohash 获取某个地理位置的 geohash 值

geoadd

API: geo key longitude latitude member [longitude latitude member ..]

Function: Add geographic location information

Demo:


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]

Function: Add geographic location information

Demo:


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

Function: Get the distance between two geographical locations

# unit: m (meters), km (kilometers), mi (miles), ft (feet)

Demo:


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]

Function: Get the distance between two geographical locations

Parameters:

withcoord: Return results include latitude and longitude withdist: Return result with location from center node withhash: geohash is included in the returned result COUNT count: Specify the number of results returned ascdesc: The returned results are arranged in ascending or reverse order according to the distance from the central node store key: Save the geographic location information of the returned results to the formulation key storedist key: Saves the distance of the returned result from the central node to the specified key

Demo:

Query all cities within 550 kilometers from Beijing and arrange them in ascending order of distance


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]

Function: Get the hash value of a certain place

Demo:


127.0.0.1:6379> geohash cities:locations beijing
1) "wx48ypbe2q0"

Related articles: