leitom/geo
Composer 安装命令:
composer require leitom/geo
包简介
A package that leverage redis geospatial functionality for super fast searching and integrates beautifully with Laravel.
README 文档
README
This package makes it easy to add geospatial to your Laravel applications. It uses Redis built in geospatial features and combine this with your Laravel Models in an elegant way.
Installation
You can install the package via composer:
composer require leitom/geo
Usage
The package comes with a simple trait thats integrates with Laravel Eloquent models but also a facade to interact with the different geospatial functions.
Laravel Eloquent integration
All you have to do when integrating this package with one or more of your Eloquent models is to use the Leitom\Geo\HasGeoAbilities trait.
This trait will take care of keeping the Redis index in sync with your models due to the use of a model observer so you dont have to think about it.
Each time you create, save or delete a model the Redis index will be updated to reflect the changes.
use Leitom\Geo\HasGeoAbilities; class User extends Model { use HasGeoAbilities; protected $fillable = ['id', 'name', 'longitude', 'latitude']; }
Search
Here we perform a search with Longitude, Latitude and Radius.
The unit of the radius is configured in the configuration file, default km = kilometers. With the 4. argument to the geoSearch function you can specify the sorting, default ASC.
$users = User::geoSearch(-115.17258, 36.11996, 10)->get();
The package adds two attributes to every model who implements the HasGeoAbilities. We add the unit and distance wich you can display to the end user.
$user->geoUnit // km $user->geoDistance; // 0.999
The toArray() function is also overrided so that these two attributes would be loaded.
[
'geo_unit' => 'km',
'geo_distance' => 0.999,
]
Find distance between two models
To find the distance between two models you can use the geoDistanceFrom function on a given model
$userA = User::findOrFail(1); $userB = User::findOrFail(2); $userA->geoDistanceFrom($userB) // ['unit' => 'km', 'distance' => 0.999]
Get the nearest models
$userA = User::findOrFail(1); $userB = User::findOrFail(2); $users = $userA->geoNearest()->paginate(5);
Perform a search via the Geo facade
$locations = Leitom\Geo\Facades\Geo::index('cars')->search(-115.17258, 36.11996, 10);
Get the distance between two locations
$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
Get a list of the nearest locations from a given location
$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]]
Add a coordinate to an index
$locationA = new Leitom\Geo\Coordinate('my-car', -115.17087, 36.12306); Leitom\Geo\Facades\Geo::index('cars')->add($locationA);
Add multiple coordinates to an index
$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]);
Remove coordinates from an index
Leitom\Geo\Facades\Geo::index('cars')->remove('my-car'); Leitom\Geo\Facades\Geo::index('cars')->remove('my-car', 'robins-car');
Import and Remove existing models
The package include two commands to handle import and remove models from the Redis index. This is usefull if you integrate this package in an existing project.
php artisan geo:import App\User
php artisan geo:remove App\User
Testing
Ensure you have Redis installed. If you are not using the default configuration for Redis you can change environment variables in the phpunit.xml file.
composer test
When testing you own application logic you should include the Leitom\Geo\Tests\RefreshGeo trait. This will ensure that you start with a fresh Redis index inside your tests. Just like the built in RefreshDatabase trait in Laravel.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email leirvik.tommy@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
leitom/geo 适用场景与选型建议
leitom/geo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 9, 最近一次更新时间为 2019 年 08 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「geo」 「laravel」 「leitom」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 leitom/geo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 leitom/geo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 leitom/geo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Microservice RPC through message queues.
Show geo marker on a mapbox map
Show geo shape on a mapbox map with drawing capabilities
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Add a weather widget to Flarum
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-03