alexpechkarev/google-maps

为Laravel收集的Google Maps API Web Services

v11.0.0 2024-03-22 20:49 UTC

README

为从Laravel应用程序设置和调用Maps API提供方便。有关服务文档、API密钥和使用限制,请访问Google Maps API Web Services地图API服务许可条款限制

功能

依赖

注意

移除地点添加、删除和雷达搜索功能

尝试使用这些功能的地点API请求将收到错误响应

  • 地点添加
  • 地点删除
  • 雷达搜索

对于影响高级数据(Zagat)、类型参数、id和引用字段的Google Places API Web Service的弃用通知。

  • 附近搜索 - types 参数已弃用,请使用参数 type(字符串)
  • 地点详情 - reference 现已弃用,改用 placeidplaceid最初用于本包)
  • 地点添加 - 仍然使用 types 参数,如服务文档所述
  • 地点自动完成 - 仍然使用 types 参数,如服务文档所述

安装

在控制台运行以下命令

composer require alexpechkarev/google-maps

或者编辑composer.json,添加以下行并运行 composer update

"require": {
		....,
		"alexpechkarev/google-maps":"^11.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变量。有关解析返回输出的更多详细信息,请参阅处理响应

必需的参数可以指定为键值对数组

$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 ) - 通过名称加载Web服务

接受字符串作为参数,按照配置文件中指定的名称作为网络服务名称。返回对自身的引用。

\GoogleMaps::load('geocoding')
...

setEndpoint( $endpoint ) - 设置请求输出

接受字符串作为参数,jsonxml,如果省略则默认为 json。返回对自身的引用。

$response = \GoogleMaps::load('geocoding')
		->setEndpoint('json')  // return $this
		...

getEndpoint() - 获取当前请求输出

返回字符串。

$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

支持

请在 GitHub 上提交一个问题

许可证

Google Maps API Web Services for Laravel 的集合是在 MIT 许可证下发布的。有关详细信息,请参阅打包的 LICENSE 文件。