alexpechkarev / google-geocoder
适用于Laravel 4/5, Slim3等的简单Google Geocoding API v3包装器
Requires
- php: >=5.4.0
Requires (Dev)
- phpspec/phpspec: ~2.0
This package is auto-updated.
Last update: 2024-09-19 02:13:49 UTC
README
本包提供了一种简单的方法,通过Laravel 5调用Google Geocoding API v3。
请参阅Laravel的Google Maps API Web服务集合,它还包括Google Geocoding API v3。
Laravel 5
依赖项
PHP cURL 是必需的
安装
要安装,请编辑 composer.json
并添加以下行
"alexpechkarev/google-geocoder": "dev-master"
运行 composer update
配置
安装完成后,在 config/app.php
中注册Laravel服务提供者
'providers' => array( ... 'Alexpechkarev\GoogleGeocoder\GoogleGeocoderServiceProvider', )
接下来,创建一个 config/google-geocoder.php
,包含以下内容
<?php return [ /* |-------------------------------------------------------------------------- | Google Geocoder |-------------------------------------------------------------------------- | Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") | into geographic coordinates (like latitude 37.423021 and longitude -122.083739), | which you can use to place markers or position the map. | */ /* |-------------------------------------------------------------------------- | Application Key |-------------------------------------------------------------------------- | | Your application's API key. This key identifies your application for | purposes of quota management. Learn how to get a key from the APIs Console. */ 'applicationKey' => 'my-api-key', /* |-------------------------------------------------------------------------- | Request URL |-------------------------------------------------------------------------- | */ 'requestUrl' => [ 'json' => 'https://maps.googleapis.com/maps/api/geocode/json?', 'xml' => 'https://maps.googleapis.com/maps/api/geocode/xml?' ], ];
Slim 3
设置
return [ 'settings' => [ /* slim settings */ 'displayErrorDetails' => true, 'determineRouteBeforeAppMiddleware' => true, 'google' => [ 'geocoder' => [ 'applicationKey' => 'my-api-key', 'requestUrl' => [ 'json' => 'https://maps.googleapis.com/maps/api/geocode/json?', 'xml' => 'https://maps.googleapis.com/maps/api/geocode/xml?' ] ] ] ] ];
容器依赖项
use GoogleGeocoder\GoogleGeocoder; $container['GeoCoder'] = function ($container) { return new GoogleGeocoder($container->get('settings')['google']['geocoder']); };
使用
$gc = $this->container['GeoCoder'];
用法
在调用之前,请确保您已获得API密钥以标识您的应用程序,并将其添加到配置文件中。有关API密钥的更多信息,请参阅Google Geocoding API。
'applicationKey' => 'your-api-key',
创建一个键值对数组,指定您要地理编码的地址
$param = array("address"=>"76 Buckingham Palace Road London SW1W 9TQ");
使用以下命令以JSON格式接收地理编码响应,使用XML作为第一个参数以获得XML响应。
$response = \Geocoder::geocode('json', $param);
要限制您的结果到特定区域,请使用组件过滤器 组件过滤,将其过滤器添加到参数数组中。
$param = array( "address"=>"76 Buckingham Palace Road London SW1W 9TQ", "components"=>"country:GB" );
地理编码API支持通过反向地理编码将地图坐标转换为人类可读的地址,使用纬度和经度参数。有关更多详细信息,请参阅反向地理编码。要执行反向地理编码请求,请使用以下命令:
$param = array("latlng"=>"40.714224,-73.961452"); $response = \Geocoder::geocode('json', $param);
所有请求都将返回 string
值。对于响应示例、状态码、错误信息和结果,请参阅[地理编码响应] (https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses)
支持
许可证
Google Geocoder for Laravel 5 在MIT许可证下发布。有关详细信息,请参阅附带LICENSE文件。