leitom/geo

一个利用Redis地理空间功能进行超快速搜索,并且与Laravel完美集成的包。

v1.2.0 2020-04-06 06:13 UTC

This package is auto-updated.

Last update: 2024-09-06 15:35:16 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

此包可轻松将地理空间功能添加到您的Laravel应用程序中。它使用Redis内置的地理空间功能,并以优雅的方式与您的Laravel模型结合。

安装

您可以通过composer安装此包

composer require leitom/geo

用法

此包附带一个简单的特质,该特质与Laravel Eloquent模型集成,同时还提供了一个外观与不同的地理空间函数交互。

Laravel Eloquent集成

当将此包与一个或多个Eloquent模型集成时,您只需使用Leitom\Geo\HasGeoAbilities特质。由于使用了模型观察者,此特质将负责使Redis索引与模型保持同步,因此您无需担心。每次创建、保存或删除模型时,Redis索引都将更新以反映更改。

use Leitom\Geo\HasGeoAbilities;

class User extends Model
{
    use HasGeoAbilities;

    protected $fillable = ['id', 'name', 'longitude', 'latitude'];
}

搜索

在此,我们使用经度、纬度和半径进行搜索。半径的单位在配置文件中配置,默认为km = 公里。您可以通过geoSearch函数的4个参数指定排序,默认为ASC

$users = User::geoSearch(-115.17258, 36.11996, 10)->get();

此包为每个实现HasGeoAbilities特质的模型添加了两个属性。我们添加了单位和距离,您可以将它们显示给最终用户。

$user->geoUnit // km
$user->geoDistance; // 0.999

同时,toArray()函数也被重写,以便加载这两个属性。

[
    'geo_unit' => 'km',
    'geo_distance' => 0.999,
]

查找两个模型之间的距离

要查找两个模型之间的距离,您可以在给定模型上使用geoDistanceFrom函数。

$userA = User::findOrFail(1);
$userB = User::findOrFail(2);

$userA->geoDistanceFrom($userB) // ['unit' => 'km', 'distance' => 0.999]

获取最近模型

$userA = User::findOrFail(1);
$userB = User::findOrFail(2);

$users = $userA->geoNearest()->paginate(5);

通过Geo外观进行搜索

$locations = Leitom\Geo\Facades\Geo::index('cars')->search(-115.17258, 36.11996, 10);

获取两个位置之间的距离

$locationA = new Leitom\Geo\Coordinate('my-car', -115.17087, 36.12306);
$locationB = new Leitom\Geo\Coordinate('robins-car', -115.171971, 36.120609);

Leitom\Geo\Facades\Geo::index('cars')->between($locationA, $locationB); // 0.2900

获取从给定位置最近的地点列表

$locationA = new Leitom\Geo\Coordinate('my-car', -115.17087, 36.12306);

Leitom\Geo\Facades\Geo::index('cars')->from($locationA, 10, 'ASC'); // [['my-car' => 0], ['your-car' => 0.2900]]

向索引添加坐标

$locationA = new Leitom\Geo\Coordinate('my-car', -115.17087, 36.12306);

Leitom\Geo\Facades\Geo::index('cars')->add($locationA);

向索引添加多个坐标

$locationA = new Leitom\Geo\Coordinate('my-car', -115.17087, 36.12306);
$locationB = new Leitom\Geo\Coordinate('robins-car', -115.171971, 36.120609);

Leitom\Geo\Facades\Geo::index('cars')->add($locationA, $locationB);

Leitom\Geo\Facades\Geo::index('cars')->add([$locationA, $locationB]);

从索引中移除坐标

Leitom\Geo\Facades\Geo::index('cars')->remove('my-car');

Leitom\Geo\Facades\Geo::index('cars')->remove('my-car', 'robins-car');

导入和移除现有模型

此包包括两个命令来处理从Redis索引中导入和移除模型。如果您将此包集成到现有项目中,这将很有用。

php artisan geo:import App\User
php artisan geo:remove App\User

测试

请确保已安装Redis。如果您不使用Redis的默认配置,您可以在phpunit.xml文件中更改环境变量。

composer test

在测试您自己的应用程序逻辑时,您应该包含Leitom\Geo\Tests\RefreshGeo特质。这将确保在测试中从Redis索引开始时是一个全新的。就像Laravel内置的RefreshDatabase特质一样。

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件leirvik.tommy@gmail.com而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。

Laravel Package Boilerplate

此包使用Laravel Package Boilerplate生成。