arraypress/geodistance
Composer 安装命令:
composer require arraypress/geodistance
包简介
A PHP utility class for calculating geographical distances between coordinates using the Haversine formula
关键字:
README 文档
README
A powerful PHP utility class for calculating geographical distances between coordinates using the Haversine formula. This library provides a clean, intuitive interface for accurate distance calculations in both miles and kilometers, with special integration for WordPress environments.
Features
- 🌍 Multi-Unit Support: Calculate distances in miles or kilometers
- 🎯 Precision: Results rounded to 2 decimal places for practical use
- 🛡️ Input Validation: Comprehensive coordinate and unit validation
- 📐 Haversine Formula: Accurate great-circle distance calculations
- 🔒 Type Safety: Full type hinting and return type declarations
- ⚡ Simple Interface: Easy to understand and implement
- 🔄 WordPress Integration: Native WP_Error support when in WordPress environment
- 🚫 Error Handling: Flexible error handling for both WordPress and standalone use
Requirements
- PHP 7.4 or later
- WordPress
Installation
Install via Composer:
composer require arraypress/geodistance
Basic Usage
use ArrayPress\Utils\Math\GeoDistance; // New York coordinates $pointA = [ 'latitude' => 40.7128, 'longitude' => -74.0060 ]; // London coordinates $pointB = [ 'latitude' => 51.5074, 'longitude' => -0.1278 ]; // Initialize calculator $calculator = new GeoDistance( $pointA, $pointB ); $distance = $calculator->get_distance(); if ( is_wp_error( $distance ) ) { echo $distance->get_error_message(); } else { echo "Distance: $distance {$calculator->get_unit()}"; }
Point Management
Getting Points
// Get current coordinates $pointA = $calculator->get_point_a(); $pointB = $calculator->get_point_b();
Setting Points
// WordPress Environment $result = $calculator->set_point_a([ 'latitude' => 35.6762, 'longitude' => 139.6503 ]); if ( is_wp_error( $result ) ) { echo $result->get_error_message(); }
Checking if a Point is Within Radius
// Central Park, New York $centralPoint = [ 'latitude' => 40.7829, 'longitude' => -73.9654 ]; // Times Square coordinates $targetPoint = [ 'latitude' => 40.7580, 'longitude' => -73.9855 ]; // Initialize calculator with central point $calculator = new GeoDistance( $centralPoint, $targetPoint ); // WordPress Environment $radius = 2; // 2 miles radius $isWithin = $calculator->is_within_radius( $targetPoint, $radius ); if ( is_wp_error( $isWithin ) ) { echo $isWithin->get_error_message(); } else { echo $isWithin ? "Location is within {$radius} {$calculator->get_unit()} radius" : "Location is outside radius"; }
Unit Management
Getting and Setting Units
// Get current unit $currentUnit = $calculator->get_unit(); // WordPress Environment $result = $calculator->set_unit( 'km' ); if ( is_wp_error( $result ) ) { echo $result->get_error_message(); }
Error Handling
WordPress Environment
// Check for errors using WP_Error $calculator = new GeoDistance( $pointA, $pointB ); $result = $calculator->set_unit('invalid_unit'); if ( is_wp_error( $result ) ) { echo $result->get_error_message(); echo $result->get_error_code(); } // Get the last error $lastError = $calculator->get_last_error(); if ( $lastError instanceof WP_Error ) { echo $lastError->get_error_message(); }
Error Codes
The library uses the following error codes when in a WordPress environment:
invalid_coordinates: Missing latitude or longitude keysinvalid_latitude: Latitude value out of range (-90 to 90)invalid_longitude: Longitude value out of range (-180 to 180)invalid_unit: Unsupported unit of measurementcalculation_error: Error during distance calculation
Use Cases
- Distance Calculation: Calculate distances between geographical points
- Location-Based Services: Determine proximity between locations
- Travel Applications: Calculate travel distances
- Geofencing: Determine if points are within specific distances
- Delivery Services: Calculate shipping distances and zones
- WordPress Integration: Seamless integration with WordPress applications
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GPL-2.0-or-later License.
Support
arraypress/geodistance 适用场景与选型建议
arraypress/geodistance 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「coordinates」 「Geodistance」 「haversine」 「latitude-longitude」 「distance-calculator」 「distance-calculation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arraypress/geodistance 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arraypress/geodistance 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arraypress/geodistance 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Get distance between two locations using PHP.
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
Geo calculations library
Simple Yet Powerful Geo Library
Laravel package providing complete Algerian geo data (69 Wilayas and Communes) with slugs, coordinates and optimized seeders.
Utility library that provides a lightweight method to calculate the great-circle distance between two geographic coordinates using the Haversine formula.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2025-09-02