iroid/laravel-haversine
Composer 安装命令:
composer require iroid/laravel-haversine
包简介
Laravel package for calculating distances using the Haversine formula
README 文档
README
Laravel Haversine is a package that provides utilities for calculating distances between geographical points using the Haversine formula. It includes a method that can be used with Laravel's Eloquent ORM to easily calculate distances between database records and a given point.
Installation
You can install the package via Composer. Run the following command in your terminal:
composer require iroid/laravel-haversine
The package will automatically register itself.
Usage
Calculating Distance
You can use the Haversine::distance() method to calculate the distance between two geographical points using the Haversine formula. This method returns the distance in kilometers by default.
use Iroid\LaravelHaversine\Haversine; $latitudeFrom = 40.7128; // Latitude of point A $longitudeFrom = -74.0060; // Longitude of point A $latitudeTo = 34.0522; // Latitude of point B $longitudeTo = -118.2437; // Longitude of point B $distanceInKm = Haversine::distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo);
Using Different Units
You can specify the unit of measurement for the distance by passing an additional parameter to the distance() method. Supported units include kilometers (default), miles, nautical miles, and meters.
$distanceInMiles = Haversine::distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, 'miles'); $distanceInNauticalMiles = Haversine::distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, 'nautical_miles'); $distanceInMeters = Haversine::distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, 'meters');
Using with Laravel Eloquent
You can use the applyHaversine() method provided by the package with Laravel's Eloquent ORM to calculate distances between database records and a given geographical point.
use Iroid\LaravelHaversine\Haversine; $latitudeFrom = 40.7128; // Latitude of the reference point $longitudeFrom = -74.0060; // Longitude of the reference point // Example: Retrieving places sorted by distance from the reference point $places = Place::applyHaversine($latitudeFrom, $longitudeFrom) ->orderBy('distance') ->get();
Parameters for applyHaversine()
$query: The Eloquent query builder instance.$latitudeFrom: Latitude of the reference point.$longitudeFrom: Longitude of the reference point.$latitudeColumn(optional): Custom column name for latitude in the database table. Defaults to'latitude'.$longitudeColumn(optional): Custom column name for longitude in the database table. Defaults to'longitude'.$unit(optional): Unit of measurement for the distance. Supported units are kilometers (default), miles, nautical miles, and meters.
You can use these parameters to customize the behavior of the applyHaversine() method according to your database schema and requirements.
Credits
Interested to contribute or modification contact me email.
iroid/laravel-haversine 适用场景与选型建议
iroid/laravel-haversine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 280 次下载、GitHub Stars 达 11, 最近一次更新时间为 2024 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 iroid/laravel-haversine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iroid/laravel-haversine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 280
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-23