varsitynewsnetwork/google-places-api

一个用于通过文本搜索从Google Places API获取场所信息的迷你库

2.1.1 2020-09-23 20:16 UTC

README

一个迷你库,通过文本搜索或查找场所请求从Google Places API获取场所信息

Build Status

用法

$service = new PlaceService(new GuzzleAdapter());
$service->setApiKey('YOUR_KEY_HERE');

$results = $service->textSearch('Van Andel Arena');

格式化器

格式化器概念被内嵌,以便您轻松操作Google返回的数据。只需将一个可调用对象作为第二个参数传递给textSearch()findPlace()

例如,如果您只想获取第一个结果的地名

$service = new PlaceService(new GuzzleAdapter());
$service->setApiKey('YOUR_KEY_HERE');

$results = $service->textSearch('Van Andel Arena', function (results) {
    if (count($results)) {
        return $results[0]['formatted_address'];
    }

    return null;
});

该库还附带了一些标准格式化器

  1. CountryStripperFormatter:从formatted_address中移除国家
  2. LatLngFormatter:将结果格式化为包含lat, lng和地址的数组
  3. SingleResultFormatter:获取第一个结果并返回
  4. CompositeFormatter:允许运行多个格式化器

示例

$service = new PlaceService(new GuzzleAdapter());
$service->setApiKey('YOUR_KEY_HERE');

$result = $service->textSearch('Van Andel Arena', new CompositeFormatter([
    new SingleResultFormatter(),
    new CountryStripperFormatter(true),
    new LatLngFormatter(true)
]));

这将产生类似的结果

Array
(
    [address] => 130 Fulton West, Grand Rapids, MI 49503
    [lat] => 42.962433
    [lng] => -85.671566
)