php使用Redis的geo
2020-12-24 本文已影响0人
php转go
Redis GEO 主要用于存储地理位置信息,并对存储的信息进行操作,该功能在 Redis 3.2 版本新增。
现在Redis最新版已经是6.0.9了,应该没有人还在用低于redis3.2了吧
public function index()
{
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$address = [
[117.224311, 39.111515, "天津"],
[113.401955, 23.130744, "广州"],
[113.867652, 22.920035, "东莞"],
[114.063402, 22.622936, "深圳"],
[113.142882, 23.036539, "佛山"],
[110.352331, 21.289926, "湛江"],
];
foreach ($address as $value) {
#添加地理位置的坐标
$res = $redis->geoadd("city", $value[0], $value[1], $value[2]);
}
#geoadd也可以同时增加多个地理空间,需要同时添加多个时
$redis->geoadd("city", 116.40378, 39.91544, "北京", 121.473913, 31.222965, "上海"); //成功则返回2
$redis->geoadd("city", 116.40378, 39.91544, "北京", 121.473913, 31.222965, "上海", 110.352331, 21.289926, "湛江"); //成功则返回3
#获取地理位置的坐标。
print_r($redis->geopos("city", "广州"));
#同时获得多个地理位置的坐标
print_r($redis->geopos("city", "广州", "佛山"));
#计算两个位置之间的距离。
print_r($redis->geodist("city", "湛江", "广州", "km"));
//时代tit的经纬度,附近1000公里内的地址,距离由近到远排序
//第六个参数的数组中
// WITHDIST : 返回距离中心点的距离。
// WITHCOORD: 返回目标的经纬度。
//WITHHASH:返回 52位 无符号整数的
//COUNT:返回条数
//ASC|DESC 正序排序|倒序排序
$argc= ['WITHCOORD', 'WITHDIST', 'ASC', 'COUNT' => 10];
$result = $redis->georadius("city", 113.407273, 23.126116, 1000, "km",$argc);
print_r($result);
//广州,附近200公里内的地址,距离由近到远排序
$result = $redis->georadiusbymember("city", "广州", 200, "km",$argc);
print_r($result);
#返回有效的Geohash字符串
$redis->geohash("city", "深圳", "广州");
}