langleyfoxall / simple-google-maps
用于各种 Google Maps API 的简单 PHP 客户端
v1.4.0
2024-07-01 15:28 UTC
Requires
- php: >=7.0||>=8.0
- ext-json: *
- divineomega/do-file-cache: ^2.0
- divineomega/php-distance: ^1.0
- guzzlehttp/guzzle: ^6.2||^7.0
- laravel/framework: ^5.1||^6.0||^7.0||^8.0||^9.0||^10.0||^11.0
This package is auto-updated.
Last update: 2024-08-31 15:50:49 UTC
README
此包提供用于各种 Google Maps API 的简单 PHP 客户端。
安装
要安装,只需运行以下 composer 命令。
composer require langleyfoxall/simple-google-maps
请记住,如果您的框架没有这样做,请包含 vendor/autoload.php
文件。
使用方法
要使用 Simple Google Maps,您必须首先创建一个新的实例。这可以通过两种方式完成,具体取决于您是否有标准的 API key
,或者 clientName
和 cryptKey
(用于企业/高级计划)。
// Standard authentication: $simpleGoogleMaps = SimpleGoogleMapsFactory::getByKey(getenv('KEY')); // Enterprise / premium plan authentication: $simpleGoogleMaps = SimpleGoogleMapsFactory::getByClientNameAndCryptKey(getenv('CLIENT_NAME'), getenv('CRYPT_KEY'));
地理编码
要将地址转换为 GPS 坐标,请使用以下示例中的 geocode
方法。
$latLng = $simpleGoogleMaps->geocode('10 Downing St, Westminster, London SW1A UK');
如果您的输入地址不够精确,您可以选择允许返回部分匹配。您可以使用以下示例中的 allowPartialMatches
方法做到这一点。
$latLng = $simpleGoogleMaps->allowPartialMatches()->geocode('test address');
上述方法将返回一个类型为 LatLong
的对象,允许您像以下示例那样访问 GPS 坐标。
$latitude = $latLng->lat; $longitude = $latLng->long;
您还可以使用 distanceTo
方法计算两个 LatLong
对象之间的距离。距离以公里为单位返回,并使用 Haversine 公式考虑地球的曲率。
$distance = $fromCoords->distanceTo($toCoords);
反向地理编码
要从一组 GPS 坐标查找地址,请使用以下示例中的 reverseGeocode
方法。
$address = $simpleGoogleMaps->reverseGeocode(new LatLong(51.5033635, -0.1276248));
此方法将返回包含在指定坐标处找到的地址的字符串。如果找不到地址,将返回 null
。
路线
要找到两点之间的路线,请使用 directions
方法。该方法期望三个参数,即起点、终点,以及可选的由 TravelMode
枚举定义的旅行模式。
请参阅以下示例用法。
$address1 = "10 Downing St, Westminster, London SW1A UK"; $address2 = "Schott House, Drummond Rd, Stafford ST16 3EL"; $journey = $simpleGoogleMaps->directions($address1, $address2, TravelMode::DRIVING); foreach($journey as $step) { echo $step->duration.' secs '; echo "\t"; echo $step->distance.' m '; echo "\t"; echo $step->description; echo PHP_EOL; } echo 'Totals: '.$journey->duration().' secs, '.$journey->distance().' km'; echo PHP_EOL;
这将生成类似以下示例的输出。
134 secs 452 m Head north on Whitehall / A3212 toward Horse Guards Ave May be closed at certain times or days
203 secs 1029 m At the roundabout, take the 1st exit onto The Mall Parts of this road may be closed at certain times or days
121 secs 688 m Turn right onto Constitution Hill
34 secs 141 m Turn left onto Duke of Wellington Pl Leaving toll zone
20 secs 83 m Turn right onto Grosvenor Pl
21 secs 107 m Slight right onto Piccadilly May be closed at certain times or days
164 secs 1244 m Slight left onto Park Ln / A4202
35 secs 199 m Slight left onto Cumberland Gate
16 secs 68 m Turn right onto Bayswater Rd
92 secs 410 m Slight left onto Edgware Rd / A5 Entering toll zone in 280 m at Upper Berkeley St Leaving toll zone in 300 m at Stourcliffe St
48 secs 177 m Turn right onto George St Entering toll zone
152 secs 531 m Turn left onto Seymour Pl
46 secs 231 m Turn left onto Marylebone Rd / A501 Leaving toll zone
722 secs 9209 m Keep right to continue on Marylebone Flyover / A40 Continue to follow A40
708 secs 14581 m Keep right to continue on Western Ave / A40
4636 secs 140047 m Keep right to continue on M40 , follow signs for M25 / Birmingham / Oxford / Beaconsfield
49 secs 1164 m At junction 3A , take the M42 / Railway Station / Airport exit to M1 / M6 / Birmingham (E, N & C) / Solihull / N.E.C.
856 secs 22091 m Merge onto M42
14 secs 375 m Keep right at the fork to continue on M6 Toll
1121 secs 33363 m Keep right at the fork to stay on M6 Toll Toll road
756 secs 19905 m Continue onto M6
23 secs 206 m At junction 14 , take the A34 exit to Stone / Stafford (N)
103 secs 1213 m At the roundabout, take the 3rd exit onto A34 Go through 1 roundabout
80 secs 925 m At the roundabout, take the 2nd exit onto Beaconside / A513
128 secs 1550 m Turn right onto Common Rd
13 secs 123 m Turn left onto Astonfields Rd
76 secs 395 m Turn left onto Drummond Rd Destination will be on the left
Totals: 10371 secs, 250507 km