ndrd/google-maps-lumen

Google Maps API Web Services for Laravel的集合


README

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

特性

依赖

注意

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

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

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

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

  • 附近搜索 - types参数已弃用,请使用参数type(字符串)
  • 地点详情 - reference现在已弃用,改为使用placeidplaceid最初在此包中使用)
  • 地点添加 - 仍然使用根据服务文档指定的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',

如果您想为任何服务使用不同的密钥,您可以在选定服务的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 ) - 通过名称加载Web服务

接受字符串作为参数,根据配置文件中指定的名称设置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() - 执行Web服务请求(无论请求类型是POST还是GET)
  • get( $key ) - 接受字符串响应体键,对于深层嵌套数组使用 '点' 表示法

返回Web服务响应,格式由 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文件。