承接 lounisbou/php-distance 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lounisbou/php-distance

最新稳定版本:v1.0.0

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.

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固