teampickr/php-google-maps-distance-matrix
Composer 安装命令:
composer require teampickr/php-google-maps-distance-matrix
包简介
PHP Implementation of the Google Maps Distance Matrix API
README 文档
README
This is a simple package that allows access to the Google Maps Distance Matrix API using a (mostly) fluent API. There is support for both the Standard License and Premium/Enterprise License types provided by Google.
Installation
Install the package using composer:
$ composer require teampickr/php-google-maps-distance-matrix
Frameworks
At the moment we only have framework compatibility for Laravel. However, we welcome PRs to add further framework specific behavior as long as it doesn't prevent the package working for others, or pull in dependencies that are not optional (suggested).
Laravel
If you are using Laravel then you can use our service provider. If you have Laravel >5.5 then the package
will be auto discovered upon install. Else, add the following to your config/app.php file:
<?php 'providers' => [ ... \TeamPickr\DistanceMatrix\Frameworks\Laravel\DistanceMatrixServiceProvider::class, ]
Facades
Personally, I hate facades. However, I know people like them. If you are using Laravel >5.5 then the facade will
be automatically discovered. Else, you can add it in your config/app.php file.
<?php 'aliases' => [ ... 'DistanceMatrix' => \TeamPickr\DistanceMatrix\Frameworks\Laravel\DistanceMatrix::class, ]
Configuration
First, make sure you have copied the configuration file:
$ php artisan vendor:publish --tag=config
This will make a config/google.php file, this is where your API Key / License information is fetched from.
By default we use the .env configuration values to get your API key.
If you have a standard api key all you need to add to your .env is:
GOOGLE_MAPS_KEY=MY-API-KEY
If you are a Premium / Enterprise Google Maps user, and use a encryption key and client ID then you should add
the following to your .env:
GOOGLE_LICENSE_TYPE=premium
GOOGLE_MAPS_CLIENT_ID=MY-CLIENT-ID
GOOGLE_MAPS_ENC_KEY=MY-ENCRYPTION-KEY
Please, make sure you don't store your keys in version control!
Usage
License / API Key
Before making requests you need to create your License object. If you are a standard google maps user, then all you will need is your API key, then you can create your license as follows:
$license = new StandardLicense($apiKey);
If you are a Premium / Enterprise google maps user, then you need to create your license as follows:
$license = new PremiumLicense($clientId, $encryptionKey);
Then, you can start making your request:
$request = new DistanceMatrix($license); // or $request = DistanceMatrix::license($license);
Basic usage
$response = DistanceMatrix::license($license) ->addOrigin('norwich,gb') ->addDestination('52.603669, 1.223785') ->request(); // I want to make the following but of API better, // as it looks horrible at the moment. $rows = $response->rows(); $elements = $rows[0]->elements(); $element = $element[0]; $distance = $element->distance(); $distanceText = $element->distanceText(); $duration = $element->duration(); $durationText = $element->durationText(); $durationInTraffic = $element->durationInTraffic(); $durationInTrafficText = $element->durationInTrafficText(); // or $response->json['destination_addresses'][0]; $response->json['rows'][0]['elements'][0]['distance']['value']; $response->json['rows'][0]['elements'][0]['duration_in_traffic']['text'];
teampickr/php-google-maps-distance-matrix 适用场景与选型建议
teampickr/php-google-maps-distance-matrix 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 140.85k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2018 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 teampickr/php-google-maps-distance-matrix 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 teampickr/php-google-maps-distance-matrix 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 140.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-13