lounisbou/php-distance
Composer 安装命令:
composer require lounisbou/php-distance
包简介
A PHP library for calculating the distance between two geographical points on Earth using various Earth radius values.
README 文档
README
php-distance is a simple PHP library for calculating the distance between two geographical points on Earth. It supports different Earth radius values and various distance calculation formulas.
Installation
Use Composer to install the library:
composer require lounisbou/phpdistance
Usage
Define Points
Define geographical points using the Point class:
use PHPDistance\Point; $start = new Point(52.5200, 13.4050); // Berlin $end = new Point(48.8566, 2.3522); // Paris
Calculate Distance Using Haversine Formula
Use the Route class with the HaversineCalculator to calculate the distance using the Haversine formula:
use PHPDistance\Route; use PHPDistance\HaversineCalculator; use PHPDistance\Enums\EarthRadius; $start = new Point(52.5200, 13.4050); // Berlin $end = new Point(48.8566, 2.3522); // Paris $calculator = new HaversineCalculator(EarthRadius::MEAN_RADIUS); $route = new Route($start, $end, $calculator); $distance = Route::getHumanReadableDistance($haversineCalculator->calculate($route)); echo "Distance using Haversine formula with mean radius: " . $distance;
Calculate Distance Using Vincenty Formula
Use the Route class with the VincentyCalculator to calculate the distance using Vincenty's formula:
use PHPDistance\Route; use PHPDistance\VincentyCalculator; $start = new Point(52.5200, 13.4050); // Berlin $end = new Point(48.8566, 2.3522); // Paris $calculator = new VincentyCalculator(); $route = new Route($start, $end, $calculator); $distance = Route::getHumanReadableDistance($vincentyCalculator->calculate($route)); echo "Distance using Vincenty formula: " . $distance;
Distance Calculation Formulas
Haversine Formula
The Haversine formula is an equation giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is useful for calculating the shortest distance over the earth's surface.
Vincenty Formula
Vincenty's formulae are two related iterative methods used in geodesy to calculate the distance between two points on the surface of an ellipsoid. They are accurate for long distances.
MySQL Formula
The MySQL formula is a simplified version of the Haversine formula that can be used in MySQL queries to calculate distances between two points.
Custom Earth Radius
You can specify a custom Earth radius if needed:
$customRadius = 6356752.3142; // Custom radius in meters $calculator = new HaversineCalculator($customRadius); $route = new Route($start, $end, $calculator); echo "Distance using Haversine formula with custom radius: " . $route->calculateDistance() . " meters\n";
Adding More Formulas
You can extend the library by adding more distance calculation formulas. Implement the DistanceCalculatorInterface to create a new calculator:
use PHPDistance\Point; use PHPDistance\DistanceCalculatorInterface; class NewFormulaCalculator implements DistanceCalculatorInterface { public function calculate(Point $from, Point $to): int { // Implement the new formula here return 0; // Placeholder } }
Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an Issue on GitHub.
License
This library is licensed under the MIT License. See the LICENSE file for details.
lounisbou/php-distance 适用场景与选型建议
lounisbou/php-distance 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「distance」 「geographical」 「earth」 「radius」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lounisbou/php-distance 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lounisbou/php-distance 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lounisbou/php-distance 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Get distance between two locations using PHP.
Geographical functionality for ORM and ODM entities and JavaScript maps rendering
Laravel 5.2 package that provides basic geographical data like Countries, Regions and Cities.
Simple class for basic google maps distance calculations.
GeoNames.org geographical data and associated functionality
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 35
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-24