metasyntactical/google-directions-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

metasyntactical/google-directions-client

Composer 安装命令:

composer require metasyntactical/google-directions-client

包简介

Client for Google Directions API to add interpolated points to a route consisting of given coordinates.

README 文档

README

Build Status Downloads this Month Latest stable Latest dev License Code Climate Test Coverage SensioLabsInsight

What is MetaSyntactical/GoogleDirections?

MetaSyntactical/GoogleDirections is a PHP client library for the Google Maps Directions API. Its purpose is to calculate a route between given geographical coordinates.

Prerequisites for using the Google Directions API

To use this library you will need a Google API key which can be obtained from Google. Usage of the Google API is subject to Terms and Conditions set out by Google. Depending on the way of you want to use Google's API and the amount of traffic/requests Google may charge fees. Please refer to Google Maps Directions API for further details.

Installation

The easiest way to install the library is adding it as a dependency to your project's composer.json file.

$ composer require metasyntactical/google-directions-client "^2"

If you use PHP version 5.5.x, 5.6.x or 7.0.x you can revert to an earlier compatible version of the library:

$ composer require metasyntactical/google-directions-client "^1"

Usage

To use the library create an instance of the client class:

use MetaSyntactical\GoogleDirections\Client as GoogleApiClient;
use MetaSyntactical\GoogleDirections\Polyline as Polyline;
use MetaSyntactical\GoogleDirections\RouteFactory;
use GuzzleHttp\Client as HttpClient;
use MetaSyntactical\Http\Transport\Guzzle\GuzzleTransport;

$guzzleTransport = new GuzzleTransport(new HttpClient());

$polylineDecoder = new Polyline();

$googleApiClient = new GoogleApiClient(
    'string', // <= your Google API key
    $polylineDecoder,
    $guzzleTransport
);

The client class has a few dependencies that must be passed to the constructor:

  • the GuzzleTransport object, which contains the HTTP client and is used by the Google API client to handle the HTTP communication
  • the Polyline object, which has a method to decode Google Polylines (kudos to Peter Chng)

You can optionally set a logger on both the Google API Client object and the Polyline object to collect messages. The logger object you pass must implement the PSR-3 logger interface.

$googleApiClient->setLogger($logger);
$polylineDecoder->setLogger($logger);

The client uses a Route object as a value object to handle the geographical coordinates. The easiest way to obtain a route object is via the factory, which will take an array of comma-separated coordinates (lat,long) for which you want to calculate a route:

$coordinates = array(
    '50.1109756,8.6824697',
    '50.1131057,8.6935646',
    '50.1114651,8.704576',
    '50.1128467,8.7049644'
);
$routeFactory = new RouteFactory();
$route = $routeFactory->createRoute($coordinates);

Note that the order of coordinates is relevant, meaning the route will start at the first given coordinate and pass through any additional coordinate in the given order up to the last given coordinate.

As with the Polyline object, you can pass a PSR-3 logger to the route factory to collect messages:

$routeFactory->setLogger($logger);

To get an interpolated route call the getDirections() method of the Google API client and pass the route object as an argument:

$resultRoute = $googleApiClient->getDirections($route);

The return value will be a Route object. Depending on the size of the route (number of coordinates), one API call (i.e. one call of the getDirections() method will only yield a partial result. This is due to the fact that the Google API handles only a limited number of waypoints per request.

You can check if there are coordinates left in the route for which additional calls to the Google API need to be made. In order to do so use the getRemainingCoordinateCount() method.

$resultRoute->getRemainingCoordinateCount();

If the count is greater than zero, just call the getDirections() method again with the result route as an argument and the Google API is called again yielding another portion of the route:

$resultRoute = $googleApiClient->getDirections($route);

Once there are no more remaining coordinates, all coordinates that make up the interpolated route can be retrieved by calling the getInterpolatedRoute() method on the result route object.

$resultRoute->getInterpolatedRoute();

Note that the getInterpolatedRoute() method returns an array of Coordinate objects, that have methods to get the values for latitude and longitude:

$coordinate->getLatitude();
$coordinate->getLongitude();

metasyntactical/google-directions-client 适用场景与选型建议

metasyntactical/google-directions-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 278 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「routing」 「client」 「geography」 「Google Maps Directions API」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 metasyntactical/google-directions-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 metasyntactical/google-directions-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 metasyntactical/google-directions-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 278
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-23