xeops / google-geolocation-bundle
为您的 Symfony2 项目提供 Google 地理位置集成
Requires
- php: >=7.0
- kriswallsmith/buzz: >=0.9
This package is auto-updated.
Last update: 2024-09-20 01:21:43 UTC
README
概述
一个为 Google 地理编码 API 服务的 Symfony 2 扩展包。
要求
- PHP 5.3+
依赖
安装
-
将包和 Buzz 库依赖添加到
vendor
目录-
使用 vendors 脚本
将以下内容添加到
deps
文件[Buzz] git=git://github.com/kriswallsmith/Buzz.git target=/Buzz [GoogleGeolocationBundle] git=git://github.com/dsyph3r/GoogleGeolocationBundle.git target=/bundles/Google/GeolocationBundle
运行 vendors 脚本
$ php bin/vendors install
-
使用 git 子模块
$ git submodule add git://github.com/kriswallsmith/Buzz.git vendor/Buzz $ git submodule add git://github.com/dsyph3r/GoogleGeolocationBundle.git vendor/bundles/Google/GeolocationBundle
-
-
将 Google 和 Network 命名空间添加到您的自动加载器
// app/autoload.php $loader->registerNamespaces(array( // .. 'Buzz' => __DIR__.'/../vendor/Buzz/lib', 'Google' => __DIR__.'/../vendor/bundles', ));
-
将包添加到应用程序内核
// app/ApplicationKernel.php public function registerBundles() { return array( // ... new Google\GeolocationBundle\GoogleGeolocationBundle(), ); }
使用方法
该包提供了一个可通过 google_geolocation.geolocation_api
标识符访问的服务。
从容器中检索服务
$geo = $this->get('google_geolocation.geolocation_api');
基本用法
查找地址
$geolocationApi = $this->get('google_geolocation.geolocation_api');
$location = $geolocationApi->locateAddress("Wales, UK");
if ($location->getMatches() > 0)
{
$matches = json_decode($location->getResult(), true);
// Get address components [city, country, postcode, etc] for 1st match
$components = $location->getAddressComponents(0);
// Get LatLng for 2nd match
$latLng = $location->getLatLng(1);
}
附加用法
该服务可以通过两种方式使用
- 不使用缓存层(默认)
- 使用缓存层
不使用缓存层
默认情况下,服务配置为不使用缓存层。
使用缓存层
缓存层提供对 Google 地理编码 API 的先前请求的缓存,以减少服务所需的请求数量。它还允许限制对服务的请求。如果您大量使用 Google 地理编码 API,这两个功能都非常有用。
要启用缓存层的使用,您需要配置服务。更新 app/config/config.yml
中的配置,如下所示
services:
google_geolocation.geolocation_api:
class: %google_geolocation.geolocation_api.class%
calls:
- [ setEntityManager, [ @doctrine.orm.entity_manager ] ]
- [ setDailyLimit, [ %google_geolocation.geolocation_api.daily_limit% ] ]
- [ setCacheLifetime, [ %google_geolocation.geolocation_api.cache_lifetime% ] ]
参数的默认值如下
google_geolocation.geolocation_api.daily_limit: 2500 # Daily requests
google_geolocation.geolocation_api.cache_lifetime: 24 # Hours
清除缓存
应定期清理缓存以符合 Google 服务条款(见下文)
运行以下任务
$ php app/console google:geolocation:clean-cache
待办事项
- 探索使用 Zend 缓存层代替 DB - Zend 缓存提供了 SQLite 选项,这将对某些项目非常有用。
Google 服务条款
请尊重 Google 对使用地理编码 API 的服务条款(TOS)
地理编码 API 服务必须与 Google 地图一起使用。该包提供的缓存功能是为了在临时缓存中使用,以增强使用地理编码时的用户体验(这是允许的)。您应定期运行清理缓存任务以清理缓存值。每个地理编码结果的有效期可以通过参数 google_geolocation.geolocation_api.cache_lifetime
设置。默认设置为 24 小时