nrqd / google-maps
Google Maps API Web Services 的 Laravel 集成包
Requires
- ext-curl: *
- ext-json: *
- alexpechkarev/geometry-library: ^1.0.2 | ^1.0
- illuminate/config: ^8.0 | ^9.0 | ^10.0
- illuminate/support: ^8.0 | ^9.0 | ^10.0
- jbroadway/urlify: ^1.1|dev-voku-portable-ascii-2.0
Requires (Dev)
- phpunit/phpunit: 4.7.* | ^9.5.10
This package is auto-updated.
Last update: 2024-09-15 16:46:10 UTC
README
为从 Laravel 应用程序设置并请求 Maps API 提供方便的方式。有关服务文档、API 密钥和用法限制,请访问 Google Maps API Web Services 和 Maps API 服务许可限制条款。
特性
依赖
注意
尝试使用这些功能的地点 API 请求将收到错误响应
- 地点添加
- 地点删除
- 雷达搜索
Google Places API Web 服务的弃用通知影响高级数据(Zagat)、类型参数、id 和引用字段。
- 附近搜索 -
types
参数已弃用,请使用参数type
(字符串) - 地点详情 -
reference
现已弃用,改为placeid
(placeid
最初用于本包) - 地点添加 - 仍然使用
types
参数,如 服务文档 所述 - 地点自动完成 - 仍然使用
types
参数,如 服务文档 所述
安装
在控制台中执行以下命令
对于 Laravel 6,请使用 6.0
。
composer require alexpechkarev/google-maps
或者编辑 composer.json,添加以下行并运行 composer update
"require": { ...., "alexpechkarev/google-maps":"^8.0", },
配置
在 'config/app.php' 中注册包服务提供者和外观
'providers' => [ ... GoogleMaps\ServiceProvider\GoogleMapsServiceProvider::class, ] 'aliases' => [ ... 'GoogleMaps' => GoogleMaps\Facade\GoogleMapsFacade::class, ]
使用 php artisan vendor:publish --tag=googlemaps
发布配置文件或简单地将包配置文件复制并粘贴到 config/googlemaps.php
打开配置文件 config/googlemaps.php
并添加您的服务密钥
/* |---------------------------------- | Service Keys |------------------------------------ */ 'key' => 'ADD YOUR SERVICE KEY HERE',
如果您想为任何服务使用不同的密钥,您可以在所选 Web 服务中的 service
数组中指定它以覆盖主 API 密钥。
用法
以下是一个向地理编码 API 发送请求的示例
$response = \GoogleMaps::load('geocoding') ->setParam (['address' =>'santa cruz']) ->get();
默认情况下,在适当的地方,output
参数设置为 JSON
。别忘了将 JSON 字符串解码为 PHP 变量。有关解析返回输出的详细信息,请参阅 处理响应。
可以指定所需参数为 key:value
对的数组
$response = \GoogleMaps::load('geocoding') ->setParam ([ 'address' =>'santa cruz', 'components' => [ 'administrative_area' => 'TX', 'country' => 'US', ] ]) ->get();
或者可以使用 setParamByKey()
方法设置参数。对于深层嵌套数组,请使用如下示例中的 "点" 表示法。
$endpoint = \GoogleMaps::load('geocoding') ->setParamByKey('address', 'santa cruz') ->setParamByKey('components.administrative_area', 'TX') //return $this ...
另一个示例显示了对地点 API 地点添加服务的请求
$response = \GoogleMaps::load('placeadd') ->setParam([ 'location' => [ 'lat' => -33.8669710, 'lng' => 151.1958750 ], 'accuracy' => 0, "name" => "Google Shoes!", "address" => "48 Pirrama Road, Pyrmont, NSW 2009, Australia", "types" => ["shoe_store"], "website" => "http://www.google.com.au/", "language" => "en-AU", "phone_number" => "(02) 9374 4000" ]) ->get();
可用方法
load( $serviceName )
setEndpoint( $endpoint )
getEndpoint()
setParamByKey( $key, $value)
setParam( $parameters)
get()
get( $key )
containsLocation( $lat, $lng )
isLocationOnEdge( $lat, $lng, $tolrance)
load( $serviceName )
- 通过名称加载网络服务
接受字符串作为参数,网络服务名称根据配置文件指定。返回对其自身的引用。
\GoogleMaps::load('geocoding') ...
setEndpoint( $endpoint )
- 设置请求输出
接受字符串作为参数,json
或xml
,如果省略则默认为json
。返回对其自身的引用。
$response = \GoogleMaps::load('geocoding') ->setEndpoint('json') // return $this ...
返回字符串。
$endpoint = \GoogleMaps::load('geocoding') ->setEndpoint('json') ->getEndpoint(); echo $endpoint; // output 'json'
setParamByKey( $key, $value )
- 使用键值对设置请求参数
接受两个参数
key
- 请求体参数名称value
- 请求体参数值
深层嵌套数组可以使用'点'符号来分配值。返回对其自身的引用。
$endpoint = \GoogleMaps::load('geocoding') ->setParamByKey('address', 'santa cruz') ->setParamByKey('components.administrative_area', 'TX') //return $this ...
setParam( $parameters)
- 一次性设置所有请求参数
接受参数数组。返回对其自身的引用。
$response = \GoogleMaps::load('geocoding') ->setParam([ 'address' => 'santa cruz', 'components' => [ 'administrative_area' => 'TX', 'country' => 'US', ] ]) // return $this ...
get()
- 执行网络服务请求(无论请求类型是POST还是GET)get( $key )
- 接受字符串响应体键,对于深层嵌套数组使用'点'符号
以setEndpoint()
方法指定的格式返回网络服务响应,如果省略则默认为JSON
。使用json_decode()
将JSON字符串转换为PHP变量。有关解析返回输出的更多详细信息,请参阅处理响应。
$response = \GoogleMaps::load('geocoding') ->setParamByKey('address', 'santa cruz') ->setParamByKey('components.administrative_area', 'TX') ->get(); var_dump( json_decode( $response ) ); // output /* {\n "results" : [\n {\n "address_components" : [\n {\n "long_name" : "277",\n "short_name" : "277",\n "types" : [ "street_number" ]\n },\n ... */
带有$key
参数的示例
$response = \GoogleMaps::load('geocoding') ->setParamByKey('latlng', '40.714224,-73.961452') ->get('results.formatted_address'); var_dump( json_decode( $response ) ); // output /* array:1 [▼ "results" => array:9 [▼ 0 => array:1 [▼ "formatted_address" => "277 Bedford Ave, Brooklyn, NY 11211, USA" ] 1 => array:1 [▼ "formatted_address" => "Grand St/Bedford Av, Brooklyn, NY 11211, USA" ] ... */
isLocationOnEdge( $lat, $lng, $tolrance = 0.1 )
- 要确定一个点是否在多边形的内部或附近,或在多边形内部或附近的边缘上,传递该点、多边形和可选的容差值(以度为单位)。
此方法仅适用于Google Maps Directions API。
接受的参数
$lat
- 双精度纬度$lng
- 双精度经度$tolrance
- 双精度
$response = \GoogleMaps::load('directions') ->setParam([ 'origin' => 'place_id:ChIJ685WIFYViEgRHlHvBbiD5nE', 'destination' => 'place_id:ChIJA01I-8YVhkgRGJb0fW4UX7Y', ]) ->isLocationOnEdge(55.86483,-4.25161); dd( $response ); // true
containsLocation( $lat, $lng )
- 要确定给定点是否在多边形内。
此方法仅适用于Google Maps Directions API。
接受的参数
$lat
- 双精度纬度$lng
- 双精度经度
$response = \GoogleMaps::load('directions') ->setParam([ 'origin' => 'place_id:ChIJ685WIFYViEgRHlHvBbiD5nE', 'destination' => 'place_id:ChIJA01I-8YVhkgRGJb0fW4UX7Y', ]) ->containsLocation(55.86483,-4.25161); dd( $response ); // true
支持
许可证
Google Maps API Web Services for Laravel的集合在MIT许可证下发布。有关详细信息,请参阅捆绑的LICENSE文件。