ixudra / geo
Laravel 5 框架的自定义 PHP 地理位置服务库 - 由 Ixudra 开发
Requires
- php: >=5.4.0
- ixudra/curl: ~6.0
README
Laravel 5 框架的自定义 PHP 地理位置服务库 - 由 Ixudra 开发。
此包可以在任何时间被任何人使用,但请注意,它针对我的个人定制工作流程进行了优化。它可能不适合您的项目,可能需要进行修改。
安装
通过 Composer 拉取此包。
{ "require": { "ixudra/geo": "6.*" } }
或者,在终端中运行:composer require ixudra/geo
Laravel 5.* 集成
将服务提供者添加到您的 config/app.php
文件中
'providers' => array( //... Ixudra\Geo\GeoServiceProvider::class, ),
将外观添加到您的 config/app.php
文件中
'aliases' => array( //... 'Geo' => Ixudra\Geo\Facades\Geo::class, ),
将以下环境值添加到您的 .env
文件中
GEO_SERVICE=google # Valid options are: google, mapquest
GEO_GOOGLE_API_KEY=your_api_key # Only required when using Google for geo coding
GEO_MAPQUEST_API_KEY=your_api_key # Only required when using MapQuest for geo coding
默认情况下,包将使用 Google API。
Lumen 5.* 集成
在您的 bootstrap/app.php
中,确保您已取消以下行的注释(大约在第 26 行)
$app->withFacades();
然后,注册您的类别名
class_alias('Ixudra\Geo\Facades\Geo', 'Geo');
最后,您必须注册您的 ServiceProvider(大约在第 70-80 行)
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
// $app->register('App\Providers\AppServiceProvider');
// Package service providers
$app->register(Ixudra\Geo\GeoServiceProvider::class);
用法
地理编码
安装包后,您可以在代码中开始使用它
use Ixudra\Geo\Facades\Geo; $response = Geo::geocode('Mersenhovenstraat 5, 3722 Kortessem'); // Will return the following response: // // { // "status": "success" // "lat": 50.8565248 // "lng": 5.391962 // }
为了使用此功能,您需要通过 Google API 控制台启用对 Google Maps API 的 API 访问
如果 GeoCoder 实例找不到任何结果,它将抛出 Ixudra\Geo\Exceptions\EmptyResponseException
。如果 GeoCoder 实例遇到某种错误,将抛出 Ixudra\Geo\Exceptions\ErrorResponseException
距离计算
安装包后,您可以在代码中开始使用它
use Ixudra\Geo\Facades\Geo; $response = Geo::distance('Mersenhovenstraat 5, 3722 Wintershoven', 'Kempische Steenweg 293, 3500 Hasselt') // Will return the following response: // // { // "status": "success" // "distance": 13556 // in meters // "duration": 1120 // in seconds // }
为了使用此功能,您需要通过 Google API 控制台启用对 Google Distance Matrix API 的 API 访问
注意:距离计算功能尚未为 MapQuest 实现。目前只能使用 Google API 服务。
如果 GeoCoder 实例找不到任何结果,它将抛出 Ixudra\Geo\Exceptions\EmptyResponseException
。
就这么多!祝您玩得开心!
许可证
此模板是开源软件,许可协议为 MIT 许可证
联系
Jan Oris(开发者)
- 电子邮件:jan.oris@ixudra.be
- 电话:+32 496 94 20 57