martijnoud / distancematrix
使用 Google 的 Distance Matrix API 获取两个地址之间距离的 API 封装器
v1.2.0
2017-02-08 14:25 UTC
This package is not auto-updated.
Last update: 2024-09-28 17:43:53 UTC
README
这是一个非常简单的 Google DistanceMatrix API 的 API 封装器。输入两个地址,类将返回它们之间的距离(米)。或者使用 Google Static Map API 来生成带有两点间线条的地图。
安装
使用 composer 安装
$ composer require martijnoud/distancematrix
基本用法
计算 Inktweb.nl 办公室和海牙的 Paleis Noordeinde 之间的距离。
use MartijnOud\DistanceMatrix\DistanceMatrix; $distanceMatrix = new DistanceMatrix(YOUR_API_KEY_HERE); $distance = $distanceMatrix->distance([ 'origins' => 'Prof. van der Waalsstraat 2 Alkmaar', 'destinations' => 'Paleis Noordeinde Den Haag' ]); if ($distance > 0) { echo round($distance / 1000, 2) . "km"; // 84.5km }
更多控制
use MartijnOud\DistanceMatrix\DistanceMatrix; $distanceMatrix = new DistanceMatrix(YOUR_API_KEY_HERE); $distance = $distanceMatrix->distance([ 'origins' => 'Leith', 'destinations' => 'Arques', 'mode' => 'walking', 'language' => 'en-GB', ]); if ($distance > 0) { echo "I would walk " . $distance * 0.00062137119 . " miles"; // I would walk 493.88322020532 miles }
生成地图
此操作不需要 API 密钥。如果您提供密钥,请确保此密钥具有使用 Static Map API 的权限。
use MartijnOud\DistanceMatrix\DistanceMatrix; $distanceMatrix = new DistanceMatrix(); $image = $distanceMatrix->map([ 'origins' => 'Prof. van der Waalsstraat 2 Alkmaar', // required 'destinations' => 'Amsterdam', // required 'size' => '728x200' ]); if ($image) { echo '<img src="'.$image.'" />'; }
待办事项
未来版本的想法。
- 更好的错误处理,检查速率限制等。
- 显示两点之间带有线条的地图。