dreadlokeur/google-geolocation-bundle

为您的 Symfony2 项目提供 Google 地理位置集成

安装: 139

依赖项: 0

建议者: 0

安全: 0

星级: 0

关注者: 1

分支: 16

类型:symfony-bundle

dev-master 2017-09-28 19:49 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:47:39 UTC


README

概述

一个用于 Google 地理编码 API 服务的 Symfony 2 扩展包。

需求

  • PHP 5.3+

依赖

安装

  1. 将扩展包和 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
      
  2. 将 Google 和 Network 命名空间添加到您的自动加载器

     // app/autoload.php
     $loader->registerNamespaces(array(
         // ..
         'Buzz'      => __DIR__.'/../vendor/Buzz/lib',
         'Google'    => __DIR__.'/../vendor/bundles',
     ));
    
  3. 将扩展包添加到应用内核

     // 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);
}

附加用法

服务可以通过两种方式使用

  1. 无缓存层(默认)
  2. 有缓存层

无缓存层

服务默认配置为不使用缓存层。

有缓存层

缓存层提供对 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 缓存层代替数据库 - Zend 缓存提供了 SQLite 选项,这可能对某些项目很有用。

Google 服务条款

请尊重 Google 为使用地理编码 API 规定的 服务条款(TOS)。

地理编码 API 服务必须与 Google 地图一起使用。扩展包提供的缓存功能是为了在临时缓存中使用,以提高使用地理编码时的用户体验(这是由 TOS 允许的)。您应定期运行清理缓存任务以清理缓存值。每个地理编码结果的生存期可以通过参数 google_geolocation.geolocation_api.cache_lifetime 设置。默认情况下,设置为 24 小时。