varsitynewsnetwork / google-places-api
一个用于通过文本搜索从Google Places API获取场所信息的迷你库
2.1.1
2020-09-23 20:16 UTC
Requires (Dev)
- guzzlehttp/guzzle: ^6.0
- peridot-php/leo: ^1.4
- peridot-php/peridot: ^1.15
- peridot-php/peridot-prophecy-plugin: ^1.1
- peridot-php/peridot-watcher-plugin: ^1.3
- squizlabs/php_codesniffer: ^2.3
Suggests
- guzzlehttp/guzzle: ^6.0
This package is auto-updated.
Last update: 2024-09-19 22:06:12 UTC
README
一个迷你库,通过文本搜索或查找场所请求从Google Places API获取场所信息
用法
$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; });
该库还附带了一些标准格式化器
CountryStripperFormatter
:从formatted_address
中移除国家LatLngFormatter
:将结果格式化为包含lat, lng和地址的数组SingleResultFormatter
:获取第一个结果并返回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
)