ivand4re / google-maps-lumen-v9
Google Maps API Web Services 的 Laravel 集成
Requires
- ext-curl: *
- ext-json: *
- alexpechkarev/geometry-library: ^1.0.2
- illuminate/config: ^9.43
- illuminate/support: ^9.43
- jbroadway/urlify: ^1.1
Requires (Dev)
- phpunit/phpunit: 4.7.*
This package is not auto-updated.
Last update: 2024-10-02 13:12:07 UTC
README
为从 Laravel 应用程序设置和向 Maps API 发送请求提供便捷方式。有关服务文档、API 密钥和用法限制,请访问 Google Maps API Web Services 和 Maps API 服务条款许可限制。
特性
依赖
注意
尝试使用这些功能的地点 API 请求将收到错误响应
- 地点添加
- 地点删除
- 雷达搜索
Google Places API Web Service 的弃用通知,影响 Premium 数据(Zagat)、types 参数、id 和 reference 字段。
- 附近搜索 -
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 文件。