vpro/geocoding
Composer 安装命令:
composer require vpro/geocoding
包简介
A PHP package to convert addresses to latitude and longitude coordinates using OpenStreetMap
README 文档
README
A simple and efficient PHP package for converting addresses into latitude and longitude coordinates using OpenStreetMap's Nominatim service. No API key required!
Features
- Convert addresses to latitude/longitude coordinates
- Batch geocoding support
- Reverse geocoding (coordinates to address)
- No API key required
- Rate limiting built-in
- Exception handling
- PSR-4 compliant
- Fully tested
Requirements
- PHP 7.4 or higher
- Composer
ext-jsonPHP extension- GuzzleHttp 7.0 or higher
Installation
Install the package via Composer:
composer require vpro/geocoding
Basic Usage
use Geocoding\Geocoder; // Initialize the geocoder with your application name $geocoder = new Geocoder('YourApp/1.0'); // Convert address to coordinates try { $coordinates = $geocoder->geocode('1600 Amphitheatre Parkway, Mountain View, CA'); if ($coordinates) { echo "Latitude: {$coordinates['lat']}\n"; echo "Longitude: {$coordinates['lng']}\n"; } } catch (GeocodingException $e) { echo "Error: " . $e->getMessage(); }
Batch Geocoding
$addresses = [ 'Empire State Building, NY', 'Golden Gate Bridge, SF' ]; $results = $geocoder->batchGeocode($addresses); foreach ($results as $address => $coordinates) { if ($coordinates) { echo "$address:\n"; echo "Latitude: {$coordinates['lat']}\n"; echo "Longitude: {$coordinates['lng']}\n"; } }
Reverse Geocoding
$address = $geocoder->reverseGeocode(37.4224764, -122.0842499); if ($address) { echo "Address: {$address['address']}\n"; echo "Details: " . print_r($address['details'], true); }
Important Notes
-
Usage Policy: This package uses OpenStreetMap's Nominatim service. Please respect their usage policy:
- Maximum of 1 request per second
- Set a meaningful User-Agent header
- For heavy usage, consider hosting your own Nominatim instance
-
Error Handling: All methods may throw
GeocodingExceptionon failure. Always wrap calls in try-catch blocks.
Testing
Run the test suite:
./vendor/bin/phpunit
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
vpro/geocoding 适用场景与选型建议
vpro/geocoding 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vpro/geocoding 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vpro/geocoding 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-31