After Redis 3.2, geographic coordinate data management is supported. Add the given coordinate elements (latitude, longitude, name) to the specified key. These data will be stored in an ordered set. You can calculate the distance between two coordinate points through GEORADIUS, or query the data within a coordinate radius using GEORADIUS and GEORADIUS by member. The following is a brief introduction to the commonly used commands.
1. GEOADD
Add coordinate information to the specified key.
GEOADD key longitude latitude member [longitude latitude member ...]
Longitude: longitude of coordinate point, valid value [- 180180]
Latitude: coordinate point latitude, valid value [- 85.05112878, 85.05112878] ]
Example:
127.0.0.1:6379> geoadd parks 116.300892 40.009462 yuanmingyuan (integer) 1 127.0.0.1:6379> geoadd parks 116.188797 39.990132 xiangshan 116.255916 40.030233 baiwangshan (integer) 2 127.0.0.1:6379> GEOADD parks 110 89.9 nanji (error) ERR invalid longitude,latitude pair 110.000000,89.900000
It can be seen that GEOADD supports adding one or more locations at a time. Each location needs to contain the precision dimension and name. When trying to add coordinates outside the legal longitude and latitude (Nanji with dimension of 89.9) When, an error will be reported.
2. GEOPOS
The GEOPOS command returns the corresponding latitude and longitude according to the name of the location. When it does not exist in the collection, the returned data is empty
Example:
127.0.0.1:6379> GEOPOS parks xiangshan baiwangshan nanji 1) 1) "116.18879646062850952" 2) "39.99013224137585354" 2) 1) "116.2559160590171814" 2) "40.03023406483775659" 3) (nil)
3. GEODIST
GEODIST Returns the distance between two given locations. If one of the two locations does not exist, the command returns null.
GEODIST key member1 member2 [unit]
unit: | m (default) | km | mi | ft |
explain: | rice | Kilometer | mile | foot |
Example:
127.0.0.1:6379> GEODIST parks xiangshan baiwangshan "7251.9492" 127.0.0.1:6379> GEODIST parks xiangshan baiwangshan km "7.2519" 127.0.0.1:6379> GEODIST parks xiangshan nanji km (nil)
4. GEORADIUS
Take the given longitude and latitude as the center, and return all position elements whose distance from the center does not exceed the given maximum distance among the position elements contained in the key.
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
The longitude, latitude and unit parameters are consistent with the previous commands. Description of other optional parameters
- WITHCOORD: return the longitude and latitude of coordinate data together
- WITHDIST: returns the distance between the coordinate data and the current coordinate
- With hash: returns the ordered set score of the location element encoded by the original geohash in the form of a 52 bit signed integer
- COUNT: returns the specified number of pieces of data
- ASC|DESC: returns the position element from near to far or from far to near according to the position of the center
- STORE and STOREDIST: save the returned results to an ordered set. The difference is that STORE stores hash values and STOREDIST stores distances
Example:
Return additional information
127.0.0.1:6379> GEORADIUS parks 116.29248 40.048107 20 km 1) "xiangshan" 2) "baiwangshan" 3) "yuanmingyuan" 127.0.0.1:6379> GEORADIUS parks 116.29248 40.048107 20 km WITHCOORD WITHDIST WITHHASH 1) 1) "xiangshan" 2) "10.9353" 3) (integer) 4069878528493207 4) 1) "116.18879646062850952" 2) "39.99013224137585354" 2) 1) "baiwangshan" 2) "3.6941" 3) (integer) 4069880373231506 4) 1) "116.2559160590171814" 2) "40.03023406483775659" 3) 1) "yuanmingyuan" 2) "4.3576" 3) (integer) 4069880708898691 4) 1) "116.30089133977890015" 2) "40.00946202493697257"
Specify the number and sort of data
127.0.0.1:6379> GEORADIUS parks 116.29248 40.048107 20 km WITHDIST ASC 1) 1) "baiwangshan" 2) "3.6941" 2) 1) "yuanmingyuan" 2) "4.3576" 3) 1) "xiangshan" 2) "10.9353" 127.0.0.1:6379> GEORADIUS parks 116.29248 40.048107 20 km WITHDIST DESC 1) 1) "xiangshan" 2) "10.9353" 2) 1) "yuanmingyuan" 2) "4.3576" 3) 1) "baiwangshan" 2) "3.6941" 127.0.0.1:6379> GEORADIUS parks 116.29248 40.048107 20 km WITHDIST ASC COUNT 2 1) 1) "baiwangshan" 2) "3.6941" 2) 1) "yuanmingyuan" 2) "4.3576"
Save the result to a new zset, hash value or distance
127.0.0.1:6379> GEORADIUS parks 116.29248 40.048107 20 km ASC COUNT 2 STORE store_20km (integer) 2 127.0.0.1:6379> GEORADIUS parks 116.29248 40.048107 20 km ASC COUNT 2 STOREDIST storedist_20km (integer) 2 127.0.0.1:6379> ZRANGE store_20km 0 -1 WITHSCORES 1) "baiwangshan" 2) "4069880373231506" 3) "yuanmingyuan" 4) "4069880708898691" 127.0.0.1:6379> ZRANGE storedist_20km 0 -1 WITHSCORES 1) "baiwangshan" 2) "3.6941190849982757" 3) "yuanmingyuan" 4) "4.3576262236174665"
5. GEORADIUSBYMEMBER
Like the GEORADIUS command, this command can find the elements within the specified range, but the center point of GEORADIUS bymember is determined by the given location element, rather than using the entered longitude and latitude to determine the center point, as GEORADIUS does.
Example:
GEORADIUSBYMEMBER parks yuanmingyuan 5 km ASC 1) "yuanmingyuan" 2) "baiwangshan"
Query the park within 5km of Yuanmingyuan