devshaded/nvdb-speed-limits
最新稳定版本:v1.0.1
Composer 安装命令:
composer require devshaded/nvdb-speed-limits
包简介
Fetch Norwegian speed limits from NVDB API
README 文档
README
A Laravel package for fetching speed limits from the Norwegian NVDB API.
Table of Contents
- Features
- Installation
- Configuration
- Usage
- Error Handling
- Testing
- Contributing
- Changelog
- Credits
- License
Features
- Fetches speed limits from the Norwegian NVDB API for given coordinates
- Supports searching with an expanding radius if no speed limit is found initially
- Batch lookup for multiple coordinates
- Configurable API endpoint, search radius, and coordinate bounds
- Designed for Laravel, but can be used in any PHP project
Installation
Install the package via Composer:
composer require devshaded/nvdb-speed-limits
Publish the config file (optional):
php artisan vendor:publish --tag="nvdb-speed-limits-config"
Configuration
The configuration file allows you to customize API settings, search parameters, and coordinate bounds. Example:
return [ 'api' => [ 'base_url' => 'https://nvdbapiles-v3.atlas.vegvesen.no', 'timeout' => 30, 'headers' => [ 'accept' => 'application/vnd.vegvesen.nvdb-v3-rev1+json', 'X-Client' => 'LaravelNvdbSpeedLimits/1.0', ], ], 'search' => [ 'default_radius' => 0.0001, // ~11 meters 'max_radius' => 0.005, // ~550 meters 'radius_multiplier' => 3, // For expanding search ], 'bounds' => [ 'latitude' => [57, 72], 'longitude' => [4, 32], ], ];
Usage
Import the facade or use the class directly:
use DevShaded\NvdbSpeedLimits\Facades\NvdbSpeedLimits; // or use DevShaded\NvdbSpeedLimits\NvdbSpeedLimits;
Get Speed Limit for a Coordinate
$result = NvdbSpeedLimits::getSpeedLimit(59.9139, 10.7522); if ($result->found) { echo "Speed limit: " . $result->recommended['speed'] . " km/h"; } else { echo "No speed limit found."; }
Optional: Specify a Search Radius
$result = NvdbSpeedLimits::getSpeedLimit(59.9139, 10.7522, 100); // 100 meters
Get Speed Limit with Expanded Search
This method will automatically expand the search radius until a speed limit is found or the maximum radius is reached.
$result = NvdbSpeedLimits::getSpeedLimitWithExpandedSearch(59.9139, 10.7522); if ($result->found) { echo "Speed limit: " . $result->recommended['speed'] . " km/h"; }
Get Speed Limits for Multiple Coordinates
You can pass an array of coordinates (with lat/lng or latitude/longitude keys):
$coordinates = [ ['lat' => 59.9139, 'lng' => 10.7522], ['latitude' => 60.3913, 'longitude' => 5.3221], ['lat' => 61.1234, 'lng' => 11.5678], ]; $results = NvdbSpeedLimits::getSpeedLimitsForCoordinates($coordinates); foreach ($results as $item) { if ($item['error']) { echo "Error for coordinate (" . json_encode($item['coordinate']) . "): " . $item['error'] . "\n"; } else { echo "Speed limit at (" . $item['coordinate']['lat'] . ", " . $item['coordinate']['lng'] . "): " . $item['result']->recommended['speed'] . " km/h\n"; } }
Error Handling
- If invalid coordinates are provided, an
InvalidCoordinatesExceptionwill be thrown. - For batch lookups, errors are included in the result array for each coordinate.
- Always check the
foundproperty on the result object to determine if a speed limit was found.
Testing
Run the tests with:
composer test
Contributing
Contributions are welcome! Please open issues or submit pull requests for improvements or bug fixes.
- Fork the repository
- Create your feature branch (
git checkout -b feature/my-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/my-feature) - Create a new Pull Request
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-01