Redis provides the geographical location information (GEO) function, with which you can complete functions such as nearby people and shake. First, let’s introduce the relevant APIs of GEO.
GEO API
Add address location information
# #geoadd key longitude latitude member [longitude latitude member ...]
127.0.0.1:6379> geoadd location 117.230279 31.81676 a 117.229704 31.824676 b (integer) 2 127.0.0.1:6379> geoadd location 117.300419 31.696095 c (integer) 1 127.0.0.1:6379> geoadd location 117.192909 31.732465 d (integer) 1 127.0.0.1:6379> geoadd location 117.189604 31.838297 e (integer) 1
Get the distance between two locations
##geodist key member1 member2 [unit]unit has four units
Now let’s take a look at the distance between Little A and Little B
127.0.0.1:6379> GEODIST location a b km "0.8821"
You can see that there is 0.88 kilometers between Little A and Little B
Let’s take a look again The distance between Little C and Little E
127.0.0.1:6379> GEODIST location c e km "18.9728"
The difference between them is nearly 19 kilometers.
Get address location information
geopos key member [member ...]come Look at the longitude and latitude information of Xiao D’s address
127.0.0.1:6379> geopos location d 1) 1) "117.19290822744369507" 2) "31.73246441933707018"
Get the geographical information location collection within the specified location range
georadius key longitude latitude radiusm km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key] georadiusbymember key member radiusm km|ft|mi [withcoord] [withdist] [ withhash] [COUNT count] [asc|desc] [store key] [storedist key]These two commands are slightly more complicated than the others. Let's take a look at these two commands together.
The functions of these two commands are basically similar. The main difference is that the first command gives the specific longitude and latitude, while the second command only gives the member name. For example, I want to know the distance between members and Dashu Mountain in Hefei. Because the longitude and latitude information of Dashu Mountain has not been stored in redis, we need to use the first command to input the longitude and latitude of Dashu Mountain. For another example, to determine the distance of other members from the coordinates of Little A, you can use the second command and directly enter member Little A.
radiusm and the following units are required information, specifying the radius distance to search within.The coordinates of Hefei Dashu Mountain are 117.175571,31.846746
# 查看离大蜀山10km的成员有哪些 127.0.0.1:6379> GEORADIUS location 117.175571 31.846746 10 km 1) "e" 2) "a" 3) "b"
You can see that small e, small a and small b are relatively close to Dashu Mountain, within 10km.
WITHCOORD: Return the longitude and latitude of the location element as well127.0.0.1:6379> GEORADIUS location 117.175571 31.846746 10 km withcoord
1) 1) "e"
2) 1) "117.18960374593734741"
2) "31.83829663190295634"
2) 1) "a"
2) 1) "117.23027676343917847"
2) "31.81675910621205361"
3) 1) "b"
2) 1) "117.22970277070999146"
2) "31.8246750403926697"
You can see that in addition to the members, the member’s location information page is also included Given
127.0.0.1:6379> GEORADIUS location 117.175571 31.846746 10 km withcoord withdist
1) 1) "e"
2) "1.6252"
3) 1) "117.18960374593734741"
2) "31.83829663190295634"
2) 1) "a"
2) "6.1522"
3) 1) "117.23027676343917847"
2) "31.81675910621205361"
3) 1) "b"
2) "5.6737"
3) 1) "117.22970277070999146"
2) "31.8246750403926697"
You can see that small E is 1.62 kilometers away from Dashu Mountain, and small A is 1.62 kilometers away from Dashu Mountain. Shushan is 6.15 kilometers away, and Little B is 5.67 kilometers away from Shushan.
COUNT count: Specify the number of returned results.
asc|desc: The returned results are in ascending or descending order according to the distance from the center node.
storedist key: Save the distance of the returned result from the center node to the specified key.
# 获取离大蜀山100km内范围的成员,按距离的升序,只需给出最近的4个成员即可
127.0.0.1:6379> GEORADIUS location 117.175571 31.846746 100 km withdist count 4 asc
1) 1) "e"
2) "1.6252"
2) 1) "b"
2) "5.6737"
3) 1) "a"
2) "6.1522"
4) 1) "d"
2) "12.8164"
Practical combatAfter introducing the above knowledge, you can use php combined with redis to complete the shake to find people nearby function. First, save the location information of the members.
伪代码如下:
function addLocation ($key,$member, $lng, $lat) { $redis->geoadd($key, $lng, $lat, $member); }
然后,获取附近的人的信息
function near ( $key, $member, $radius, $unit = 'km', $count = 0, $withDist = false, $withcoord = false, $orderby = 'ASC' ) { $redis = new Redis(); $redis->connect('localhost', 6379); $options = [$orderby]; if ($count > 0) { $options['count'] = $count; } if ($withDist) { $options[] = 'WITHDIST'; } if ($withcoord) { $options[] = 'WITHCOORD'; } $result = $redis->geoRadiusByMember($key, $member, $radius, $unit, $options); return $result; }
使用redis可以大大方便开发人员,丰富的API可以完成各种各样的需求,Redis的使用已经成为程序员必备的技能了。
The above is the detailed content of Use Redis to complete the WeChat shake function. For more information, please follow other related articles on the PHP Chinese website!