alex-kalanis / google-maps-php-services
Google Maps API Web Services 的 PHP 客户端库(SDK)
Requires
- php: >=8.1
- psr/http-client: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: >=10 <11
- shipmonk/composer-dependency-analyser: ^1.4
This package is auto-updated.
Last update: 2024-09-04 09:31:08 UTC
README
PHP 的 Google Maps 服务 for
PHP 客户端库(SDK)用于 Google Maps API Web Services。
基于更早的 Nick Tsai 库的分支。
差异
- 最低 PHP 版本为 8.1
- 类型检查
- 依赖注入
- 考虑到 PSR 和其他远程库(如 Guzzle/Curl)
- 现在只可用 API 密钥
概要
演示
适用于几乎所有原始 PHP/自定义框架
// somewhere in configuration something like this /// ... with anonymous function as setter in DI function (): \kalanis\google_maps\ClientConfig { return \kalanis\google_maps\ClientConfig::init('Your API Key'); }
然后在所需的页面上
class YourPresenter extends YourFramework { public function __construct( // ... other used classes protected kalanis\google_maps\Client $mapService, ) { } public function process(): void { // Geocoding an address $geocodeResult = $this->mapService->geocode('Pelješki most, Croatia'); // Look up an address with reverse geocoding $reverseGeocodeResult = $this->mapService->reverseGeocode(42.916667, 17.533333); // Request directions via public transit $directionsResult = $this->mapService->directions('Ploče', 'Dubrovnik', [ 'mode' => "transit", 'departure_time' => time(), ]); } }
对于 Laravel
/// app\Providers\AppServiceProvider.php public function register() { // ... other binds $this->app()->bind(\kalanis\google_maps\Client::class, function( Psr\Http\Client\ClientInterface $client, Psr\Http\Message\RequestInterface $request ) { return new \kalanis\google_maps\Client( $request, $client, \kalanis\google_maps\ClientConfig::init('Your API Key'), ); } ); }
然后在控制器中的代码与另一个随机框架相同。
对于 Symfony
# config/services.yaml services: kalanis\google_maps\ClientConfig: arguments: ['Your API Key'] kalanis\google_maps\Client: arguments: ['@Psr\Http\Message\RequestInterface', '@Psr\Http\Client\ClientInterface', '@kalanis\google_maps\ClientConfig']
namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; #[Route('/your-maps')] class YourMapsPresenter extends AbstractController { public function __construct( // ... other used classes protected kalanis\google_maps\Client $mapService, ) { } #[Route('/geocode', name: 'maps_geocode', defaults: ['where' => 'Pelješki most, Croatia'], methods: ['GET'])] public function geocode(Request $request, string $where): Response { $geocodeResult = $this->mapService->geocode($where); return new JsonResponse($geocodeResult) } #[Route('/reverse-geocode', lat: '42.916667', lon: '17.533333', defaults: ['lat' => '42.916667', 'lon' => '17.533333'], methods: ['GET'])] public function reverseGeocode(Request $request, string $lat, string $lon): Response { $geocodeResult = $this->mapService->reverseGeocode([$lat, $lon]); return new JsonResponse($geocodeResult) } }
对于 Nette
services: # ... other ones - kalanis\google_maps\ClientConfig('Your API Key') - kalanis\google_maps\Client # ... other ones
或具有不同服务器/服务的参数
parameters: # ... other ones googleMapsKey: 'Your API Key' # ... other ones services: # ... other ones - kalanis\google_maps\ClientConfig(%parameters.googleMapsKey%) - kalanis\google_maps\Client # ... other ones
然后在类中,如其他框架中的 DI。
描述
Google Maps 服务的 PHP 客户端是以下 Google Maps APIs 的 PHP 客户端库
- 地图
- 路线
- 地点
要求
- PHP 8.1+ 或更高版本
API 密钥
每个Google Maps Web Service请求都需要一个API密钥或客户端ID。您可以在https://developers.google.com/console使用Google帐户免费获取API密钥。您需要的API密钥类型是服务器密钥。
获取API密钥
-
访问https://developers.google.com/console并使用Google帐户登录。
-
选择您的一个现有项目,或创建一个新的项目。
-
启用您计划使用的Google Maps服务API,例如
- 方向 API
- 距离矩阵 API
- 地理编码 API
- 地点API
- 道路API
- 时区 API
- 附近 API
-
创建一个新的服务器密钥。
-
如果您想将请求限制为特定的IP地址,现在就可以这样做。
对于指导帮助,请遵循路线API的说明。您只需要一个API密钥,但请记住启用您需要的所有API。获取更多信息,请参阅API密钥指南。
重要:此密钥应保存在您的服务器上,并保密。
安装
在您的项目中运行Composer
composer require alex-kalanis/google-maps-php-services
然后根据您的PHP框架在Composer加载后调用它
require __DIR__ . '/vendor/autoload.php'; use kalanis\google_maps\Client;
使用方法
在开始使用任何Google Maps服务之前,您首先需要创建一个具有配置的客户端,然后使用客户端访问Google Maps服务。
客户端
使用API密钥创建客户端
$gmaps = new \kalanis\google_maps\Client( new \PsrMock\Psr7\Request(), new \PsrMock\Psr18\Client(), new \kalanis\google_maps\ConfigClient('Your API Key'), );
语言
您可以为所有服务设置客户端的语言
$gmaps = new \kalanis\google_maps\Client( new \PsrMock\Psr7\Request(), new \PsrMock\Psr18\Client(), new \kalanis\google_maps\ConfigClient('Your API Key', 'pt-br'), );
API
海拔 API
// Get elevation by locations parameter $elevationResult = $gmaps->elevation(25.0339639, 121.5644722); $elevationResult = $gmaps->elevation('25.0339639', '121.5644722');
路线 API
$routes = $gmaps->computeRoutes($originArray, $destinationArray, $fullBodyArray, $fieldMask) // Get the route data between two places simply $routes = $gmaps->computeRoutes([ "location" => [ "latLng" => [ "latitude" => 37.419734, "longitude" => -122.0827784 ] ] ], [ "location" => [ "latLng" => [ "latitude" => 37.41767, "longitude" => -122.079595 ] ] ]); // Get the full route data between two places with full request data $routes = $gmaps->computeRoutes([...], [...], ["travelMode": "DRIVE", ...], '*');
道路API
$roads = $gmaps->snapToRoads([[-35.27801,149.12958], [-35.28032,149.12907], [-35.28099,149.12929]]);
方向 API
// Request directions via public transit $directionsResult = $gmaps->directions('Milano', 'Venezia', [ 'mode' => "transit", 'departure_time' => time(), ]);
距离矩阵 API
// Get the distance matrix data between two places $distanceMatrixResult = $gmaps->distanceMatrix('Canberra', 'Perth'); // With Imperial units $distanceMatrixResult = $gmaps->distanceMatrix('Stonehenge', 'Bristol', [ 'units' => 'imperial', ]);
地理编码 API
// Geocoding an address $geocodeResult = $gmaps->geocode('Avenida Maracanã 350, Rio de Janeiro'); // Look up an address with reverse geocoding $reverseGeocodeResult = $gmaps->reverseGeocode(-22.912167, -43.230164);
地理位置 API
// Simple geolocate $geolocateResult = $gmaps->geolocate([]);
时区 API
// requests the time zone data for given location $timezoneResult = $gmaps->timezone([25.381111, 83.021389]); // Sárnáth
附近 API
// requests the nearby points for given location $nearbyResult = $gmaps->nearby('restaurant', [25.71874, 32.6574]); // in Luxor
通过地点查找 API
// requests the place points for given location by given place $nearbyResult = $gmaps->findPlace('Champs Elysees', 'restaurant', ['name', 'current_opening_hours']);
通过文本查找 API
// requests the place points for given location by given text $nearbyResult = $gmaps->findText('Sagrada Familia', 350, [], 3, 0, true);
地点详情 API
// requests the details about place point $nearbyResult = $gmaps->placeDetails('ChIJN1t_tDeuEmsRUsoyG83frY4', ['name', 'current_opening_hours']);