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.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-31