概述
RedisGeo實現方式:
1、使用geohash保存地理位置的座標。
2、使用有序集合(zset)保存地理位置的集合。
geohash的特點:
1、字符串越長,表示的範圍越精確。編碼長度爲8時,精度在19米左右,而當編碼長度爲9時,精度在2米左右。
2、字符串相似的表示距離相近,利用字符串的前綴匹配,可以查詢附近的地理位置。這樣就實現了快速查詢某個座標附近的地理位置。
3、geohash計算的字符串,可以反向解碼出原來的經緯度。
指令
geoadd:新增地址位置座標
geoadd beijing 116.111 38.111 changcheng
geoadd beijing 116.222 38.222 gugong
geopos:獲取地理位置座標
127.0.0.1:6379> geopos beijing changcheng
1) 1) "116.11100167036057"
2) "38.110998966851803"
georadius:計算位置座標附近2千米範圍內的座標點
127.0.0.1:6379> georadius beijing 116.123 38.123 2 km
1) "changcheng"
geohash:獲取geohash
127.0.0.1:6379> geohash beijing changcheng
1) "wwf8f1vkwm0"
georadiusbymember:查詢指定元素附近的其它元素
127.0.0.1:6379> georadiusbymember beijing changcheng 2 km
1) "changcheng "
2) "gugong"127.0.0.1:6379> georadiusbymember beijing changcheng 2 km count 2 desc
1) "gugong"
2) "changcheng"
zrange:獲取所有座標點
127.0.0.1:6379> zrange beijing 0 -1
1) "changcheng"
2) "gugong"
zrem :刪除指定的座標點
127.0.0.1:6379> zrem beijing changcheng
(integer) 1