jfsimon/gmap-bundle

Symfony GMap bundle

安装次数: 51,067

依赖者: 0

建议者: 0

安全: 0

星级: 19

关注者: 4

分支: 3

开放问题: 4

类型:symfony-bundle

dev-master 2012-04-23 08:28 UTC

This package is not auto-updated.

Last update: 2024-09-22 02:42:00 UTC


README

进行中 : 该组件正处于积极开发中,将定期添加新功能。

它工作正常 : 该组件处于测试驱动开发控制之下,因此以下提到的每个功能都将真正实现。

当前实现 : 目前只包含以下服务

  • 2个网络服务 :
    • 地理编码网络服务
    • 高程网络服务
  • 1个工具服务 :
    • 多段线编码器

当前分支 : 全面的重构,采用更好的方法。

基本内容

为了使用此组件,您必须安装它,然后可选地测试它(不要忘记在github上发布问题!)。

安装组件

  1. 将源添加到您的组件目录中 - 从项目的根目录开始,粘贴以下命令

    git submodule add git@github.com:alephnullplex/GMapBundle.git vendor/bundles/GMapBundle
    
  2. 在您的自动加载和AppKernel中注册组件

    // app/autoload.php in registerNamespaces()
    'GMapBundle'       => __DIR__.'/../vendor/bundles',
    
    // app/AppKernel.php in registerBundles()
    new GMapBundle\GMapBundle(),
    
  3. 在您的应用程序配置文件中注册组件配置 - 例如,在您的app/config/config.yml文件中添加以下简化的代码

    g_map:
      config: ~
    

完成!

运行测试

  1. 注册路由。在您的app/config/routing_dev.yml文件中添加以下代码 - 这将在开发区域中注册路由

    _Tests_GMapBundle:
        resource: "@GMapBundle/Resources/config/routing.yml"
    
  2. 使用phpunit运行测试,以下测试可用

    phpunit --c app/ vendor/bundles/GMapBundle/Tests/ServiceTests.php
    phpunit --c app/ vendor/bundles/GMapBundle/Tests/PolylineEncoderTests.php
    phpunit --c app/ vendor/bundles/GMapBundle/Tests/GeocoderTests.php
    phpunit --c app/ vendor/bundles/GMapBundle/Tests/ElevationTests.php
    

如何使用

此组件提供了一个可以从控制器访问的新服务。要访问该服务,请使用以下代码

$gmap = $this->get('gmap');

这些服务每天对每个服务有2,500个地理定位请求的限制(假设每个服务如此)。

网络服务

配置

每个网络服务都包含以下4个常用参数(不能被请求覆盖)

url : ~ // the webservice URL
format : 'json' // the internal response format, for now only 'json' is implemented
formatter : ~ // the result formatter class for one result
collection : ~ // the collection result fomatter class

每个网络服务还提供自己的一组参数,可以在配置(默认值)中设置

  • (作为关联数组)
  • 对于每个请求(作为关联数组)。

响应

  • 如果请求被拒绝、格式不正确或超出限制(每天2500个请求),则引发适当的异常
  • 如果请求没有返回结果,则引发ZeroResultsException
  • 如果请求返回一个结果,您将获得一个Formatter对象
  • 如果请求返回多个结果,您将获得一个Collection对象
  • Collection对象是可迭代的,为了方便起见,Formatter对象也是可迭代的。
  • 迭代一个Collection对象将给出Formatter对象。
  • 每个网络服务都返回自己的FormatterCollection对象,并具有适当的方法。

地理编码网络服务

此服务用于从地址获取纬度/经度点及其相反。它还可以用于标准化和解析地址。有关更多信息,请参阅Google网络服务文档

###获取格式化器

简单易用,以下是一些示例

// get the geocode object from an address (wich is really dirty)
$home = $gmap->geocode('12 rU hipOLYte lBAs 75009 fR');

// get the geocode object from a latitude / longitude array
$home = $gmap->geocode(array(48.8772535, 2.3397612));

// get the geocode object from a latitude / longitude string
$home = $gmap->geocode('48.8772535, 2.3397612');

作为第二个参数,您可以提供一个选项的关联数组

  • bounds:视口内要突出显示地理编码结果的边界框。
  • region:区域代码,指定为ccTLD(顶级域名)两个字符值。
  • language:返回结果的语言。
  • sensor(默认为false):指示地理编码请求是否来自具有位置传感器的设备。

更多关于这些选项的解释请参考谷歌文档

多次请求可以获得一组结果,您可以通过类型筛选地址。

// get the 'street_address' type (the more precise)
// just one address of this type, you get a `Formatter` object
$home = $home->filter('street_address');

geocode方法返回一个Geocode对象,该对象包含多个方法以获取您所需的数据。

### 使用geocoder可以做什么

获取地址的位置

$home = $this->get('gmap')->geocode('12 Rue Hippolyte Lebas 75009 France')->filter('street_address');

// get the latitude
$lat = $home->getLat(); // 48.8772535

// get the longitude
$lng = $home->getLng(); // 2.3397612

// get both as string
$str = $home->getLatLng(); // '48.8772535, 2.3397612'

// get both as an array
$arr = $home->getLatLng(true); // array(48.8772535, 2.3397612)

从位置获取地址

$home = $this->get('gmap')->geocode('48.8772535, 2.3397612')->filter('street_address');

// get the *normalized* address as string
$str = $home->getAddress(); // 12 Rue Hippolyte Lebas, 75009 Paris, France

标准化地址

// a dirty address is inputed by a user
$home = $this->get('gmap')->geocode('12 rU hipOLYte lBAs 75009 fR')->filter('street_address');

// get the *normalized* address
$str = $home->getAddress(); // 12 Rue Hippolyte Lebas, 75009 Paris, France

地址组件

$home = $this->get('gmap')->geocode('12 Rue Hippolyte Lebas 75009 France')->filter('street_address');

// get the number
$str = $home->getAddressComponent('street_number'); // '12'

// get the city
$str = $home->getAddressComponent('locality'); // 'Paris'

// get the region (for France)
$str = $home->getAddressComponent('administrative_area_level_1'); // 'Ile-de-France'

// get the zip code
$str = $home->getAddressComponent('postal_code'); // '75009'

// get a sublocality
$str = $home->getAddressComponent('sublocality'); // '9ème Arrondissement Paris'

等等... 更多组件的完整列表请参考谷歌文档。如果一个组件有多个值,它将返回一个数组。

此外,getAddressComponent方法接受第二个布尔参数,将其设置为true将返回短名称。例如

// get the country short name
$str = $home->getAddressComponent('country', true); // 'FR'

// get the region (for France) short name
$str = $home->getAddressComponent('administrative_area_level_1', true); // 'IDF'

### 配置设置

这个web服务附带3个更多选项,以下是YAML格式的示例

gmap.options:
    geocoder:
        // the 4 common parameters
        bounds : ~ # default bounds option for each requests (see above)
        region : ~ # default region option for each requests (see above)
        language : ~ # default language option for each requests (see above)
        sensor : false // whether the browser has GPS functionalities

高程网络服务

此服务用于从一系列点(纬度/经度)获取海拔高度。结果对象Formatter有4个方法

  • getLat() : 返回纬度

  • getLng() : 返回经度

  • getLatLng() : 返回一个数组

  • getElevation() : 返回海拔(浮点数)

    $elevations = $this->get('gmap')->elevation($points);

只有一个参数

gmap.options:
    elevation:
        // the 4 common parameters
        sensor : false // whether the browser has GPS functionalities

工具服务

多段线编码器

此服务由其他服务在后台使用以压缩一系列纬度/经度点。您可以通过以下方法在控制器中访问它

$encoded = $this->get('gmap')->encodePolyline($polyline);

其中 $polyline 是点的数组,每个点是一个包含2个值的数组:纬度和经度;$encoded 是一个关联数组,包含2个键:'points'(编码点)和'levels'(编码级别)。配置设置

一些选项可用,以下是YML格式的示例

gmap.options:
    polyline_encoder:
        accuracy: 5 # should not be changed !
        levels: 4 # the levels number (called numLevels in the Google's documentation)
        zoom: 3 # the zoom factor
        endpoints: true # indicate if endpoints should be forced

您可以在谷歌文档中了解更多关于这些内容。