<?php

$redis = new Redis;

$redis->connect('127.0.0.1' , 6379);
$redis->auth('123456');

$key = 'cities';
//添加测试数据
$redis->geoadd($key , '116.40' , '39.90' , 'beijing');
$redis->geoadd($key , '121.44' , '31.21' , 'shanghai');
$redis->geoadd($key , '114.27' , '30.57' , 'wuhan');
$redis->geoadd($key , '106.54' , '29.58' , 'chongqing');
$redis->geoadd($key , '112.967' , '28.19' , 'changsha');

//获取两个地点的距离,单位:m(米,默认), km(千米), mi(英里), ft(英尺)
var_dump($redis->geodist($key,'beijing','wuhan','ft'));

//获取成员经纬度
var_dump($redis->geopos($key,'beijing'));

//获取成员的经纬度hash,geohash表示坐标的一种方法,便于检索和存储
var_dump($redis->geohash($key,'shanghai', 'wuhan'));

//查询以指定经纬度方圆1000KM的成员

/**
 * 第五个参数:
 * withcoord:返回结果包含经纬度
 * withdist:返回结果包含离中心节点的距离
 * count:返回结果数量
 * asc | desc:返回结果按照离中心位置升序、降序
 * store key:返回结果地理位置信息保存在指定键中
 * storedist key:返回结果离中心节点距离保存在指定的键中
 */


var_dump($redis->georadius($key , '100' , '25' , '1000' , 'km'));

//表示获取到指定的距离
var_dump($redis->georadius($key , '100' , '25' , '1000' , 'km' , ['withcoord']));

//返回结果包含离中心节点的距离
var_dump($redis->georadius($key , '100' , '25' , '1000' , 'km' , ['withdist']));

//返回结果数量
var_dump($redis->georadius($key , '100' , '25' , '1000' , 'km' , ['count' => 3]));

//返回结果按照离中心位置升序、降序
var_dump($redis->georadius($key , '100' , '25' , '3000' , 'km' , ['asc']));//正序
var_dump($redis->georadius($key , '100' , '25' , '3000' , 'km' , ['desc']));//倒序

//大体上和georadius一样 只是经纬度改为了成员名称
var_dump($redis->georadiusbymember($key,'wuhan','3000','km'));

 

posted on 2021-10-18 15:48  沉默的土豆  阅读(69)  评论(0编辑  收藏  举报